<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="laravel,">










<meta name="description" content="事件的使用Laravel 启动流程分析1234567891011121314151617181920212223php artisan make:event UserRegisteredphp artisan make:listener SendWelcomeMail --event=UserRegisteredphp artisan make:listener UpdateReferrer --">
<meta name="keywords" content="laravel">
<meta property="og:type" content="article">
<meta property="og:title" content="laravel 笔记">
<meta property="og:url" content="http://yoursite.com/2019/03/27/laravel-笔记/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="事件的使用Laravel 启动流程分析1234567891011121314151617181920212223php artisan make:event UserRegisteredphp artisan make:listener SendWelcomeMail --event=UserRegisteredphp artisan make:listener UpdateReferrer --">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-10-15T11:53:06.296Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="laravel 笔记">
<meta name="twitter:description" content="事件的使用Laravel 启动流程分析1234567891011121314151617181920212223php artisan make:event UserRegisteredphp artisan make:listener SendWelcomeMail --event=UserRegisteredphp artisan make:listener UpdateReferrer --">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/03/27/laravel-笔记/">



<meta name="referrer" content="never"> ​​​​


  <title>laravel 笔记 | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/27/laravel-笔记/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">laravel 笔记</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-27T19:44:44+08:00">
                2019-03-27
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  28.2k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  135 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="事件的使用Laravel-启动流程分析"><a href="#事件的使用Laravel-启动流程分析" class="headerlink" title="事件的使用Laravel 启动流程分析"></a>事件的使用Laravel 启动流程分析</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">php artisan make:event UserRegistered</span><br><span class="line"></span><br><span class="line">php artisan make:listener SendWelcomeMail --event=UserRegistered</span><br><span class="line"></span><br><span class="line">php artisan make:listener UpdateReferrer --event=UserRegistered</span><br><span class="line">protected $listen = [ </span><br><span class="line">    UserRegistered::<span class="function"><span class="params">class</span> =&gt;</span> [ </span><br><span class="line">        SendWelcomeMail::<span class="class"><span class="keyword">class</span>, </span></span><br><span class="line">        UpdateReferrer::class,</span><br><span class="line">    ]</span><br><span class="line">];</span><br><span class="line">https:<span class="comment">//learnku.com/articles/26152</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        parent::boot();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">static</span>::created(<span class="function"><span class="keyword">function</span> (<span class="params">User $user</span>) </span>&#123;</span><br><span class="line">            \event(<span class="keyword">new</span> UserRegistered($user));</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="cron-timeout"><a href="#cron-timeout" class="headerlink" title="cron timeout"></a>cron timeout</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">crontab定时启动任务，flock保证互斥，timeout设置超时以及报警脚本</span><br><span class="line">flock -xn /tmp/lock /path/to/php /path/to/file</span><br><span class="line">让我们再来重放一下故障场景：假如上一分钟的 A 请求还没退出，下一分钟的 B 请求也启动了，那么 B 请求会发现 A 请求还没有释放锁，于是它不会执行。</span><br><span class="line">假如因为某些无法预知的原因，导致脚本不能正常结束请求，进而导致不能正常释放锁，那么后续所有其它的 CD 等请求也都无法执行了，如何避免？答案是 timeout，它实现了超时控制机制：</span><br><span class="line">https:<span class="comment">//huoding.com/2016/12/12/573  https://juejin.im/post/5bb4fc0de51d450e597b6d51</span></span><br><span class="line">timeout -s SIGINT <span class="number">100</span> flock -xn /tmp/lock /path/to/php /path/to/file</span><br><span class="line">让我们再来重放一下故障场景：假如上一分钟的 A 请求还没退出，下一分钟的 B 请求也启动了，那么 B 请求会发现 A 的请求还没有释放锁，于是它不会执行，不过下下分钟的 C 请求肯定能执行，因为在这之前，A 请求已经因为超时被 timeout 干掉了。</span><br><span class="line"></span><br><span class="line"># 超时发送-9信号，超时执行后面的脚本输出failed</span><br><span class="line">timeout -s <span class="number">9</span> <span class="number">5</span> sleep <span class="number">20</span> || echo <span class="string">'failed'</span></span><br><span class="line"># 未超时执行后面的脚本输出success</span><br><span class="line">timeout -s <span class="number">9</span> <span class="number">10</span> sleep <span class="number">5</span> &amp;&amp; echo <span class="string">'success'</span></span><br><span class="line"> 需要注意的点</span><br><span class="line"></span><br><span class="line">timeout 正常结束的返回码是<span class="number">0</span></span><br><span class="line">timeout 超时kill结束的返回码是<span class="number">124</span></span><br><span class="line"></span><br><span class="line"> 假设<span class="number">6</span>分钟执行一次且互斥非阻塞，超时<span class="number">10</span>分钟且超时执行报警脚本，则最终crontab文件如下</span><br><span class="line"> *<span class="regexp">/6 * * * * cd /</span>data/projec &amp;&amp; flock -xn ./task.lock -c <span class="string">'timeout -9 600 sh task.sh || sh alarm.sh'</span></span><br><span class="line"> 需要注意的点</span><br><span class="line"> 如果task.sh启动了子进程进行处理，则需要在task.sh的末尾加上wait命令等待全部子进程完成才结束，否则timeout无效</span><br><span class="line"> cd /data/project</span><br><span class="line"> nohub python3 main.py &gt;main.log <span class="number">2</span>&gt;&amp;<span class="number">1</span> &amp;</span><br><span class="line"> wait</span><br><span class="line"> https:<span class="comment">//juejin.im/post/5bb4fc0de51d450e597b6d51</span></span><br></pre></td></tr></table></figure>
<h3 id="杀死过期进程"><a href="#杀死过期进程" class="headerlink" title="杀死过期进程"></a>杀死过期进程</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">fire</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            $pids = $<span class="keyword">this</span>-&gt;getPids([<span class="string">"php"</span>, <span class="string">"shell_lock"</span>]);</span><br><span class="line">            foreach ($pids <span class="keyword">as</span> $pid) &#123;</span><br><span class="line">                $startTime = $<span class="keyword">this</span>-&gt;getProcessStartTime($pid);</span><br><span class="line">                $<span class="keyword">this</span>-&gt;ifKill($startTime, $pid);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (Exception $e) &#123;</span><br><span class="line">            Log::error(<span class="string">"["</span> . ClientIp::getIp() . <span class="string">"][LONG TIME KILLER]: "</span> . $e-&gt;getMessage() . <span class="string">"\r\n"</span> . $e-&gt;getTraceAsString());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">getPids</span>(<span class="params">$arr</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $command = <span class="string">"ps aux | grep -v grep "</span>;</span><br><span class="line">        foreach ($arr <span class="keyword">as</span> $word) &#123;</span><br><span class="line">            $command .= <span class="string">"| grep &#123;$word&#125;"</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        $command .= <span class="string">" | awk '&#123;print $2&#125;'"</span>;</span><br><span class="line">        $result = shell_exec($command);</span><br><span class="line">        $result = trim($result);</span><br><span class="line">        <span class="keyword">if</span> (empty($result)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> explode(<span class="string">"\n"</span>, $result);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">getProcessStartTime</span>(<span class="params">$pid</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">/*ps -p 18994 -o lstart</span></span><br><span class="line"><span class="comment">                           STARTED</span></span><br><span class="line"><span class="comment">          Thu Mar 21 10:25:38 2019</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">        $command = <span class="string">"ps -p &#123;$pid&#125; -o lstart"</span>;</span><br><span class="line">        $result = shell_exec($command);</span><br><span class="line">        $result = trim(str_replace(<span class="string">"STARTED"</span>, <span class="string">""</span>, $result));</span><br><span class="line">        <span class="keyword">if</span> (empty($result)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        $ctime = \Carbon\Carbon::createFromFormat(<span class="string">"D M j H:i:s Y"</span>, $result);</span><br><span class="line">        <span class="keyword">return</span> $ctime;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">ifKill</span>(<span class="params">Carbon $ctime, $pid</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($ctime-&gt;lt(\Carbon\Carbon::now()-&gt;addHours(<span class="number">-1</span>))) &#123;</span><br><span class="line">            $cmd = file_get_contents(<span class="string">"/proc/&#123;$pid&#125;/cmdline"</span>);</span><br><span class="line">            $logString =   Carbon::now()-&gt;toDateTimeString() . <span class="string">"]Killing pid &#123;$pid&#125; started at "</span> . $ctime-&gt;toDateTimeString() . <span class="string">" lasts for more than 1 hour, start script is `"</span> . $cmd . <span class="string">"`"</span>;</span><br><span class="line">            \Log::info($logString);</span><br><span class="line">            shell_exec(<span class="string">"kill -9 &#123;$pid&#125;"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> pid <span class="keyword">in</span> $(ps aux|grep <span class="string">'artisan   '</span> | grep -v grep | awk <span class="string">'&#123;print $2&#125;'</span>); <span class="keyword">do</span> kill <span class="number">-9</span> $pid; done</span><br><span class="line">ps -ef | pgrep -f <span class="string">"search"</span> | xargs kill <span class="number">-9</span></span><br></pre></td></tr></table></figure>
<h3 id="每个文章的前10条评论一同查询出来"><a href="#每个文章的前10条评论一同查询出来" class="headerlink" title="每个文章的前10条评论一同查询出来"></a>每个文章的前10条评论一同查询出来</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">$posts = Post::paginate(<span class="number">15</span>);</span><br><span class="line"></span><br><span class="line">$postIds = $posts-&gt;pluck(<span class="string">'id'</span>)-&gt;all();</span><br><span class="line"></span><br><span class="line"><span class="comment">//找出符合条件的 comments ，同时定义 @post, @rank 变量，这里没有用 all,get 等函数，此时并不会执行 SQL 语句。</span></span><br><span class="line">$sub = Comment::whereIn(<span class="string">'post_id'</span>,$postIds)-&gt;select(DB::raw(<span class="string">'*,@post := NULL ,@rank := 0'</span>))-&gt;orderBy(<span class="string">'post_id'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//把上面构造的 sql 查询作为子表进行查询，根据 post_id 进行分区的同时 @rank 变量不断+1</span></span><br><span class="line">$sub2 = DB::table( DB::raw(<span class="string">"(&#123;$sub-&gt;toSql()&#125;) as b"</span>) )</span><br><span class="line">            -&gt;mergeBindings($sub-&gt;getQuery())</span><br><span class="line">            -&gt;select(DB::raw(<span class="string">'b.*,IF (</span></span><br><span class="line"><span class="string">            @post = b.post_id ,@rank :=@rank + 1 ,@rank := 1</span></span><br><span class="line"><span class="string">        ) AS rank,</span></span><br><span class="line"><span class="string">        @post := b.post_id'</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">//取出符合条件的前10条comment</span></span><br><span class="line">$commentIds = DB::table( DB::raw(<span class="string">"(&#123;$sub2-&gt;toSql()&#125;) as c"</span>) )</span><br><span class="line">            -&gt;mergeBindings($sub2)</span><br><span class="line">        -&gt;where(<span class="string">'rank'</span>,<span class="string">'&lt;'</span>,<span class="number">11</span>)-&gt;select(<span class="string">'c.id'</span>)-&gt;pluck(<span class="string">'id'</span>)-&gt;toArray();</span><br><span class="line"></span><br><span class="line">$comments = Comment::whereIn(<span class="string">'id'</span>,$commentIds)-&gt;get();</span><br><span class="line"></span><br><span class="line">$posts = $posts-&gt;each(<span class="function"><span class="keyword">function</span> (<span class="params">$item, $key</span>) <span class="title">use</span> (<span class="params">$comments</span>) </span>&#123;</span><br><span class="line">    $item-&gt;comments = $comments-&gt;where(<span class="string">'post_id'</span>,$item-&gt;id);</span><br><span class="line">&#125;);</span><br><span class="line">会产生三条sql https:<span class="comment">//learnku.com/articles/20315</span></span><br><span class="line">select * <span class="keyword">from</span> <span class="string">`posts`</span> limit <span class="number">15</span> offset <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">select <span class="string">`c`</span>.<span class="string">`id`</span> <span class="keyword">from</span> (select b.*,IF (</span><br><span class="line">@post = b.post_id ,@rank :=@rank + <span class="number">1</span> ,@rank := <span class="number">1</span></span><br><span class="line">) AS rank,</span><br><span class="line">@post := b.post_id <span class="keyword">from</span> (select *,@post := NULL ,@rank := <span class="number">0</span> <span class="keyword">from</span> <span class="string">`comments`</span> where <span class="string">`post_id`</span> <span class="keyword">in</span> (<span class="string">'2'</span>, <span class="string">'3'</span>, <span class="string">'4'</span>, <span class="string">'5'</span>, <span class="string">'6'</span>, <span class="string">'7'</span>, <span class="string">'8'</span>, <span class="string">'9'</span>, <span class="string">'10'</span>, <span class="string">'11'</span>, <span class="string">'12'</span>, <span class="string">'13'</span>, <span class="string">'14'</span>, <span class="string">'15'</span>, <span class="string">'16'</span>) order by <span class="string">`post_id`</span> asc) <span class="keyword">as</span> b) <span class="keyword">as</span> c where <span class="string">`rank`</span> &lt; <span class="string">'11'</span>;</span><br><span class="line"></span><br><span class="line">select * <span class="keyword">from</span> <span class="string">`comments`</span> where <span class="string">`id`</span> <span class="keyword">in</span> (<span class="string">'180'</span>, <span class="string">'589'</span>, <span class="string">'590'</span>, <span class="string">'3736'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-集合"><a href="#Laravel-集合" class="headerlink" title="Laravel 集合"></a>Laravel 集合</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/laravel/t/26110</span></span><br><span class="line"></span><br><span class="line">  $users = User::all();</span><br><span class="line">        $users-&gt;contains(<span class="string">'name'</span>, <span class="string">'Chasity Tillman'</span>);</span><br><span class="line">        <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line">        $collection = collect([<span class="string">'name'</span> =&gt; <span class="string">'John'</span>, <span class="string">'age'</span> =&gt; <span class="number">23</span>]);</span><br><span class="line">        $collection-&gt;contains(<span class="string">'Jane'</span>);</span><br><span class="line">        <span class="comment">//false</span></span><br><span class="line"></span><br><span class="line">        $collection = collect([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]);</span><br><span class="line">        $collection-&gt;contains(<span class="function"><span class="keyword">function</span> (<span class="params">$key, $value</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $value &lt;= <span class="number">5</span>;</span><br><span class="line">            <span class="comment">//true</span></span><br><span class="line">        &#125;);</span><br><span class="line"> $users = User::all();</span><br><span class="line">        $youngsters = $users-&gt;filter(<span class="function"><span class="keyword">function</span> (<span class="params">$value, $key</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $value-&gt;age &lt; <span class="number">35</span>;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        $youngsters-&gt;all();</span><br><span class="line">        <span class="comment">// 所有年龄小于 35 的用户</span></span><br><span class="line">        $movies = collect([</span><br><span class="line">                    [</span><br><span class="line">                        <span class="string">'name'</span> =&gt; <span class="string">'Back To The Future'</span>,</span><br><span class="line">                        <span class="string">'releases'</span> =&gt; [<span class="number">1985</span>, <span class="number">1989</span>, <span class="number">1990</span>]</span><br><span class="line">                    ],</span><br><span class="line">                    [</span><br><span class="line">                        <span class="string">'name'</span> =&gt; <span class="string">'Fast and Furious'</span>,</span><br><span class="line">                        <span class="string">'releases'</span> =&gt; [<span class="number">2001</span>, <span class="number">2003</span>, <span class="number">2006</span>, <span class="number">2009</span>, <span class="number">2011</span>, <span class="number">2013</span>, <span class="number">2015</span>, <span class="number">2017</span>]</span><br><span class="line">                    ],</span><br><span class="line">                    [</span><br><span class="line">                        <span class="string">'name'</span> =&gt; <span class="string">'Speed'</span>,</span><br><span class="line">                        <span class="string">'releases'</span> =&gt; [<span class="number">1994</span>]</span><br><span class="line">                    ]</span><br><span class="line">                ]);</span><br><span class="line">        </span><br><span class="line">                $mostReleases = $movies-&gt;sortByDesc(<span class="function"><span class="keyword">function</span> (<span class="params">$movie, $key</span>) </span>&#123;</span><br><span class="line">                    <span class="keyword">return</span> count($movie[<span class="string">'releases'</span>]);</span><br><span class="line">                &#125;);</span><br><span class="line">        </span><br><span class="line">                $mostReleases-&gt;toArray();</span><br><span class="line">                <span class="comment">//列出以上映总数降序排序的电影</span></span><br><span class="line">        </span><br><span class="line">                dd($mostReleases-&gt;values()-&gt;toArray());</span><br><span class="line">                <span class="comment">/*</span></span><br><span class="line"><span class="comment">                   列出以上映总数降序排序的电影并重置键值</span></span><br><span class="line"><span class="comment">                */</span></span><br><span class="line">                 $movies = collect([</span><br><span class="line">                            [<span class="string">'name'</span> =&gt; <span class="string">'Back To the Future'</span>, <span class="string">'genre'</span> =&gt; <span class="string">'scifi'</span>, <span class="string">'rating'</span> =&gt; <span class="number">8</span>],</span><br><span class="line">                            [<span class="string">'name'</span> =&gt; <span class="string">'The Matrix'</span>,  <span class="string">'genre'</span> =&gt; <span class="string">'fantasy'</span>, <span class="string">'rating'</span> =&gt; <span class="number">9</span>],</span><br><span class="line">                            [<span class="string">'name'</span> =&gt; <span class="string">'The Croods'</span>,  <span class="string">'genre'</span> =&gt; <span class="string">'animation'</span>, <span class="string">'rating'</span> =&gt; <span class="number">8</span>],</span><br><span class="line">                            [<span class="string">'name'</span> =&gt; <span class="string">'Zootopia'</span>,  <span class="string">'genre'</span> =&gt; <span class="string">'animation'</span>, <span class="string">'rating'</span> =&gt; <span class="number">4</span>],</span><br><span class="line">                            [<span class="string">'name'</span> =&gt; <span class="string">'The Jungle Book'</span>,  <span class="string">'genre'</span> =&gt; <span class="string">'fantasy'</span>, <span class="string">'rating'</span> =&gt; <span class="number">5</span>],</span><br><span class="line">                        ]);</span><br><span class="line">                </span><br><span class="line">                        $genre = $movies-&gt;groupBy(<span class="string">'genre'</span>);</span><br><span class="line">                        <span class="comment">/*</span></span><br><span class="line"><span class="comment">                        [</span></span><br><span class="line"><span class="comment">                             "scifi" =&gt; [</span></span><br><span class="line"><span class="comment">                               ["name" =&gt; "Back To the Future", "genre" =&gt; "scifi", "rating" =&gt; 8,],</span></span><br><span class="line"><span class="comment">                             ],</span></span><br><span class="line"><span class="comment">                             "fantasy" =&gt; [</span></span><br><span class="line"><span class="comment">                               ["name" =&gt; "The Matrix", "genre" =&gt; "fantasy", "rating" =&gt; 9,],</span></span><br><span class="line"><span class="comment">                               ["name" =&gt; "The Jungle Book", "genre" =&gt; "fantasy", "rating" =&gt; 5, ],</span></span><br><span class="line"><span class="comment">                             ],</span></span><br><span class="line"><span class="comment">                             "animation" =&gt; [</span></span><br><span class="line"><span class="comment">                               ["name" =&gt; "The Croods", "genre" =&gt; "animation", "rating" =&gt; 8,],</span></span><br><span class="line"><span class="comment">                               ["name" =&gt; "Zootopia", "genre" =&gt; "animation", "rating" =&gt; 4, ],</span></span><br><span class="line"><span class="comment">                             ],</span></span><br><span class="line"><span class="comment">                        ]</span></span><br><span class="line"><span class="comment">                        */</span></span><br><span class="line">                </span><br><span class="line">                        $rating = $movies-&gt;groupBy(<span class="function"><span class="keyword">function</span> (<span class="params">$movie, $key</span>) </span>&#123;</span><br><span class="line">                            <span class="keyword">return</span> $movie[<span class="string">'rating'</span>];</span><br><span class="line">                        &#125;);</span><br><span class="line">                </span><br><span class="line">                        <span class="comment">/*</span></span><br><span class="line"><span class="comment">                        [</span></span><br><span class="line"><span class="comment">                           8 =&gt; [</span></span><br><span class="line"><span class="comment">                             ["name" =&gt; "Back To the Future", "genre" =&gt; "scifi", "rating" =&gt; 8,],</span></span><br><span class="line"><span class="comment">                             ["name" =&gt; "The Croods", "genre" =&gt; "animation", "rating" =&gt; 8,],</span></span><br><span class="line"><span class="comment">                           ],</span></span><br><span class="line"><span class="comment">                           9 =&gt; [</span></span><br><span class="line"><span class="comment">                             ["name" =&gt; "The Matrix", "genre" =&gt; "fantasy", "rating" =&gt; 9,],</span></span><br><span class="line"><span class="comment">                           ],</span></span><br><span class="line"><span class="comment">                           4 =&gt; [</span></span><br><span class="line"><span class="comment">                             ["name" =&gt; "Zootopia","genre" =&gt; "animation", "rating" =&gt; 4,],</span></span><br><span class="line"><span class="comment">                           ],</span></span><br><span class="line"><span class="comment">                           5 =&gt; [</span></span><br><span class="line"><span class="comment">                             ["name" =&gt; "The Jungle Book","genre" =&gt; "fantasy","rating" =&gt; 5,],</span></span><br><span class="line"><span class="comment">                           ],</span></span><br><span class="line"><span class="comment">                        ]</span></span><br><span class="line"><span class="comment">                       */</span></span><br><span class="line">                    &#125;</span><br><span class="line">                    $list = collect([</span><br><span class="line">                                <span class="string">'Albert'</span>, <span class="string">'Ben'</span>, <span class="string">'Charles'</span>, <span class="string">'Dan'</span>, <span class="string">'Eric'</span>, <span class="string">'Xavier'</span>, <span class="string">'Yuri'</span>, <span class="string">'Zane'</span></span><br><span class="line">                            ]);</span><br><span class="line">                    </span><br><span class="line">                            <span class="comment">//获取前两个名字</span></span><br><span class="line">                            $firstTwo = $list-&gt;take(<span class="number">2</span>);</span><br><span class="line">                            <span class="comment">//['Albert', 'Ben']</span></span><br><span class="line">                    </span><br><span class="line">                            <span class="comment">//获取最后两个名字</span></span><br><span class="line">                            $lastTwo = $list-&gt;take(<span class="number">-2</span>);</span><br><span class="line">                            <span class="comment">//['Yuri', 'Zane']</span></span><br><span class="line">                            $list = collect([</span><br><span class="line">                                        <span class="string">'Albert'</span>, <span class="string">'Ben'</span>, <span class="string">'Charles'</span>, <span class="string">'Dan'</span>, <span class="string">'Eric'</span>, <span class="string">'Xavier'</span>, <span class="string">'Yuri'</span>, <span class="string">'Zane'</span></span><br><span class="line">                                    ]);</span><br><span class="line">                            </span><br><span class="line">                                    $chunks = $list-&gt;chunk(<span class="number">3</span>);</span><br><span class="line">                                    $chunks-&gt;toArray();</span><br><span class="line">                                    <span class="comment">/*</span></span><br><span class="line"><span class="comment">                                    [</span></span><br><span class="line"><span class="comment">                                        ["Albert", "Ben", "Charles",],</span></span><br><span class="line"><span class="comment">                                        [3 =&gt; "Dan", 4 =&gt; "Eric", 5 =&gt; "Xavier",],</span></span><br><span class="line"><span class="comment">                                        [6 =&gt; "Yuri", 7 =&gt; "Zane",],</span></span><br><span class="line"><span class="comment">                                    ]</span></span><br><span class="line"><span class="comment">                                    */</span></span><br><span class="line">                                     $names = collect([</span><br><span class="line">                                                <span class="string">'Albert'</span>, <span class="string">'Ben'</span>, <span class="string">'Charles'</span>, <span class="string">'Dan'</span>, <span class="string">'Eric'</span>, <span class="string">'Xavier'</span>, <span class="string">'Yuri'</span>, <span class="string">'Zane'</span></span><br><span class="line">                                            ]);</span><br><span class="line">                                    </span><br><span class="line">                                            $names-&gt;transform(<span class="function"><span class="keyword">function</span> (<span class="params">$name, $key</span>) </span>&#123;</span><br><span class="line">                                                <span class="keyword">return</span> strlen($name);</span><br><span class="line">                                            &#125;);</span><br><span class="line">                                    </span><br><span class="line">                                            $names-&gt;toArray();</span><br><span class="line">                                            <span class="comment">//[6, 3, 7, 3, 4, 6, 4, 4,]</span></span><br><span class="line">                                             $names = collect([</span><br><span class="line">                                                        <span class="string">'Albert'</span>, <span class="string">'Ben'</span>, <span class="string">'Charles'</span>, <span class="string">'Dan'</span>, <span class="string">'Eric'</span>, <span class="string">'Xavier'</span>, <span class="string">'Yuri'</span>, <span class="string">'Zane'</span></span><br><span class="line">                                                    ]);</span><br><span class="line">                                            </span><br><span class="line">                                                    $names-&gt;transform(<span class="function"><span class="keyword">function</span> (<span class="params">$name, $key</span>) </span>&#123;</span><br><span class="line">                                                        <span class="keyword">return</span> strlen($name);</span><br><span class="line">                                                    &#125;);</span><br><span class="line">                                            </span><br><span class="line">                                                    $names-&gt;toArray();</span><br><span class="line">                                                    <span class="comment">//[6, 3, 7, 3, 4, 6, 4, 4,]</span></span><br></pre></td></tr></table></figure>
<h3 id="队列执行失败反复执行"><a href="#队列执行失败反复执行" class="headerlink" title="队列执行失败反复执行"></a>队列执行失败反复执行</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">由于报错或者什么原因队列执行失败，但是队列的 attempts一直为<span class="number">1</span>，可以手动<span class="keyword">throw</span></span><br><span class="line"><span class="keyword">try</span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;<span class="keyword">catch</span> (\Throwable $e) &#123;</span><br><span class="line">  <span class="keyword">throw</span> <span class="keyword">new</span> \Exception(<span class="string">"queue fail"</span>);</span><br><span class="line">&#125;</span><br><span class="line">php artisan xx --tries=<span class="number">2</span></span><br></pre></td></tr></table></figure>
<h3 id="实现Schemaless"><a href="#实现Schemaless" class="headerlink" title="实现Schemaless"></a>实现Schemaless</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br></pre></td><td class="code"><pre><span class="line">mysql&gt; CREATE TABLE users (</span><br><span class="line">           id INT UNSIGNED NOT NULL AUTO_INCREMENT,</span><br><span class="line">           created_at timestamp <span class="literal">null</span>,</span><br><span class="line">           updated_at timestamp <span class="literal">null</span>,</span><br><span class="line">           data <span class="built_in">JSON</span> NOT NULL,</span><br><span class="line">           PRIMARY KEY(id)</span><br><span class="line">       );</span><br><span class="line"></span><br><span class="line">mysql&gt; ALTER TABLE users add name VARCHAR(<span class="number">100</span>) AS</span><br><span class="line">       (JSON_UNQUOTE(JSON_EXTRACT(data, <span class="string">'$.name'</span>))) AFTER id;</span><br><span class="line"></span><br><span class="line">mysql&gt; ALTER TABLE users add address VARCHAR(<span class="number">100</span>) AS</span><br><span class="line">       (JSON_UNQUOTE(JSON_EXTRACT(data, <span class="string">'$.address'</span>))) AFTER name;</span><br><span class="line"></span><br><span class="line">mysql&gt; ALTER TABLE users add level INT UNSIGNED AS</span><br><span class="line">       (JSON_EXTRACT(data, <span class="string">'$.level'</span>)) AFTER name;</span><br><span class="line">然后是核心代码 Schemaless.php，以 trait 的方式实现：https:<span class="comment">//huoding.com/2017/01/14/590 </span></span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace App;</span><br><span class="line"></span><br><span class="line">trait Schemaless</span><br><span class="line">&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getDirty</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $dirty = collect(parent::getDirty());</span><br><span class="line"></span><br><span class="line">        $keys = $dirty-&gt;keys()-&gt;map(<span class="function"><span class="keyword">function</span>(<span class="params">$key</span>) </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (in_array($key, $<span class="keyword">this</span>-&gt;virtual)) &#123;</span><br><span class="line">                $key = $<span class="keyword">this</span>-&gt;getDataColumn() . <span class="string">'-&gt;'</span> . $key;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> $key;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $keys-&gt;combine($dirty)-&gt;all();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">save</span>(<span class="params">array $options = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (!$<span class="keyword">this</span>-&gt;exists) &#123;</span><br><span class="line">            $attributes = collect($<span class="keyword">this</span>-&gt;getAttributes());</span><br><span class="line"></span><br><span class="line">            $virtual = $attributes-&gt;only($<span class="keyword">this</span>-&gt;virtual);</span><br><span class="line"></span><br><span class="line">            $attributes = $attributes-&gt;diffKeys($virtual)-&gt;merge([</span><br><span class="line">                $<span class="keyword">this</span>-&gt;getDataColumn() =&gt; json_encode($virtual-&gt;all()),</span><br><span class="line">            ]);</span><br><span class="line"></span><br><span class="line">            $<span class="keyword">this</span>-&gt;setRawAttributes($attributes-&gt;all());</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> parent::save($options);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getDataColumn</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">static</span> $column;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($column === <span class="literal">null</span>) &#123;</span><br><span class="line">            $column = defined(<span class="string">'static::DATA'</span>) ? <span class="keyword">static</span>::DATA : <span class="string">'data'</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $column;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">接着是 Model 实现 User.php，里面激活了 schemaless，并设置了虚拟字段：</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace App;</span><br><span class="line"></span><br><span class="line">use Illuminate\Database\Eloquent\Model;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    use Schemaless;</span><br><span class="line"></span><br><span class="line">    protected $virtual = [<span class="string">'name'</span>, <span class="string">'address'</span>, <span class="string">'level'</span>];</span><br><span class="line"></span><br><span class="line">    protected $hidden = [<span class="string">'data'</span>];</span><br><span class="line">&#125;</span><br><span class="line">最后是 Controller 实现 UsersController.php，里面演示了如何创建和修改：</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace App\Http\Controllers;</span><br><span class="line"></span><br><span class="line">use Illuminate\Http\Request;</span><br><span class="line">use App\User;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UsersController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">User $user</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;user = $user;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">store</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $user = $<span class="keyword">this</span>-&gt;user;</span><br><span class="line"></span><br><span class="line">        $user-&gt;name = <span class="string">'老王'</span>;</span><br><span class="line">        $user-&gt;address = <span class="string">'东北'</span>;</span><br><span class="line">        $user-&gt;level = <span class="number">1</span>;</span><br><span class="line">        $user-&gt;save();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">update</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $user = $<span class="keyword">this</span>-&gt;user-&gt;find(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">        $user-&gt;address = <span class="string">'北京'</span>;</span><br><span class="line">        $user-&gt;save();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="PhpSpreadsheet-导出图片到-Excel"><a href="#PhpSpreadsheet-导出图片到-Excel" class="headerlink" title="PhpSpreadsheet 导出图片到 Excel"></a>PhpSpreadsheet 导出图片到 Excel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">export</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">       $spreadsheet = <span class="keyword">new</span> Spreadsheet();</span><br><span class="line">       $sheet = $spreadsheet-&gt;getActiveSheet();</span><br><span class="line">       <span class="comment">//设置sheet的名字  两种方法 https://learnku.com/articles/26965</span></span><br><span class="line">       $sheet-&gt;setTitle(<span class="string">'phpspreadsheet——demo'</span>);</span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;setTitle(<span class="string">'Hello'</span>);</span><br><span class="line">       <span class="comment">//设置第一行小标题</span></span><br><span class="line">       $k = <span class="number">1</span>;</span><br><span class="line">       $sheet-&gt;setCellValue(<span class="string">'A'</span> . $k, <span class="string">'问题'</span>);</span><br><span class="line">       $sheet-&gt;setCellValue(<span class="string">'B'</span> . $k, <span class="string">'选项'</span>);</span><br><span class="line">       $sheet-&gt;setCellValue(<span class="string">'C'</span> . $k, <span class="string">'答案'</span>);</span><br><span class="line">       $sheet-&gt;setCellValue(<span class="string">'D'</span> . $k, <span class="string">'图片'</span>);</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 设置个表格宽度</span></span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getColumnDimension(<span class="string">'A'</span>)-&gt;setWidth(<span class="number">16</span>);</span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getColumnDimension(<span class="string">'B'</span>)-&gt;setWidth(<span class="number">80</span>);</span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getColumnDimension(<span class="string">'C'</span>)-&gt;setWidth(<span class="number">15</span>);</span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getColumnDimension(<span class="string">'D'</span>)-&gt;setWidth(<span class="number">20</span>);</span><br><span class="line"></span><br><span class="line">       <span class="comment">// 垂直居中</span></span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getStyle(<span class="string">'A'</span>)-&gt;getAlignment()-&gt;setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);</span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getStyle(<span class="string">'B'</span>)-&gt;getAlignment()-&gt;setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);</span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getStyle(<span class="string">'C'</span>)-&gt;getAlignment()-&gt;setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);</span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getStyle(<span class="string">'D'</span>)-&gt;getAlignment()-&gt;setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);</span><br><span class="line"></span><br><span class="line">       $info = $data;</span><br><span class="line">       <span class="comment">//  设置A单元格的宽度 同理设置每个</span></span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getColumnDimension(<span class="string">'A'</span>)-&gt;setWidth(<span class="number">20</span>);</span><br><span class="line">       <span class="comment">//  设置第三行的高度</span></span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;getRowDimension(<span class="string">'3'</span>)-&gt;setRowHeight(<span class="number">50</span>);</span><br><span class="line">       <span class="comment">//  A1水平居中</span></span><br><span class="line">       $styleArray = [</span><br><span class="line">           <span class="string">'alignment'</span> =&gt; [</span><br><span class="line">               <span class="string">'horizontal'</span> =&gt; \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,</span><br><span class="line">           ],</span><br><span class="line">       ];</span><br><span class="line">       $sheet-&gt;getStyle(<span class="string">'A1'</span>)-&gt;applyFromArray($styleArray);</span><br><span class="line">       <span class="comment">//  将A3到D4合并成一个单元格</span></span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;mergeCells(<span class="string">'A3:D4'</span>);</span><br><span class="line">       <span class="comment">//  拆分合并单元格</span></span><br><span class="line">       $spreadsheet-&gt;getActiveSheet()-&gt;unmergeCells(<span class="string">'A3:D4'</span>);</span><br><span class="line">       <span class="comment">//  将A2到D8表格边框 改变为红色</span></span><br><span class="line">       $styleArray = [</span><br><span class="line">           <span class="string">'borders'</span> =&gt; [</span><br><span class="line">               <span class="string">'outline'</span> =&gt; [</span><br><span class="line">                   <span class="string">'borderStyle'</span> =&gt; \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,</span><br><span class="line">                   <span class="string">'color'</span> =&gt; [<span class="string">'argb'</span> =&gt; <span class="string">'FFFF0000'</span>],</span><br><span class="line">               ],</span><br><span class="line">           ],</span><br><span class="line">       ];</span><br><span class="line">       <span class="comment">//  $sheet-&gt;getStyle('A2:E8')-&gt;applyFromArray($styleArray);</span></span><br><span class="line">       <span class="comment">//  设置超链接</span></span><br><span class="line">       <span class="comment">//  $sheet-&gt;setCellValue('D6', 'www.baidu.com');</span></span><br><span class="line">       <span class="comment">//  $spreadsheet-&gt;getActiveSheet()-&gt;setCellValue('E6', 'www.baidu.com');</span></span><br><span class="line">       <span class="comment">//  循环赋值</span></span><br><span class="line">       $k = <span class="number">2</span>;</span><br><span class="line">       foreach ($info <span class="keyword">as</span> $key =&gt; $value) &#123;</span><br><span class="line">           $sheet-&gt;setCellValue(<span class="string">'A'</span> . $k, $value[<span class="string">'question'</span>]);</span><br><span class="line">           $sheet-&gt;setCellValue(<span class="string">'B'</span> . $k, $value[<span class="string">'question_options'</span>]);</span><br><span class="line">           $sheet-&gt;setCellValue(<span class="string">'C'</span> . $k, $value[<span class="string">'answer'</span>]);</span><br><span class="line"></span><br><span class="line">           $img = self::curlGet($value[<span class="string">'img'</span>]);</span><br><span class="line">           $dir = public_path(<span class="string">'/temp/image/'</span>);</span><br><span class="line">           $file_info = pathinfo($value[<span class="string">'img'</span>]);</span><br><span class="line">           <span class="keyword">if</span> (!empty($file_info[<span class="string">'basename'</span>])) &#123; <span class="comment">//过滤非文件类型</span></span><br><span class="line">               $basename = $file_info[<span class="string">'basename'</span>];</span><br><span class="line">               is_dir($dir) OR mkdir($dir, <span class="number">0777</span>, <span class="literal">true</span>); <span class="comment">//进行检测文件是否存在</span></span><br><span class="line">               file_put_contents($dir . $basename, $img);</span><br><span class="line"></span><br><span class="line">               $drawing[$k] = <span class="keyword">new</span> Drawing();</span><br><span class="line">               $drawing[$k]-&gt;setName(<span class="string">'Logo'</span>);</span><br><span class="line">               $drawing[$k]-&gt;setDescription(<span class="string">'Logo'</span>);</span><br><span class="line">               $drawing[$k]-&gt;setPath($dir . $basename);</span><br><span class="line">               $drawing[$k]-&gt;setWidth(<span class="number">80</span>);</span><br><span class="line">               $drawing[$k]-&gt;setHeight(<span class="number">80</span>);</span><br><span class="line">               $drawing[$k]-&gt;setCoordinates(<span class="string">'D'</span>.$k);</span><br><span class="line">               $drawing[$k]-&gt;setOffsetX(<span class="number">12</span>);</span><br><span class="line">               $drawing[$k]-&gt;setOffsetY(<span class="number">12</span>);</span><br><span class="line">               $drawing[$k]-&gt;setWorksheet($spreadsheet-&gt;getActiveSheet());</span><br><span class="line">           &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">               $sheet-&gt;setCellValue(<span class="string">'D'</span> . $k, <span class="string">''</span>);</span><br><span class="line">           &#125;</span><br><span class="line">           $sheet-&gt;getRowDimension($k)-&gt;setRowHeight(<span class="number">80</span>);</span><br><span class="line">           $k++;</span><br><span class="line">       &#125;</span><br><span class="line">       $file_name = date(<span class="string">'Y-m-d'</span>, time()) . rand(<span class="number">1000</span>, <span class="number">9999</span>);</span><br><span class="line">       <span class="comment">//  第一种保存方式</span></span><br><span class="line">       <span class="comment">/*$writer = new Xlsx($spreadsheet);</span></span><br><span class="line"><span class="comment">       //保存的路径可自行设置</span></span><br><span class="line"><span class="comment">       $file_name = '../'.$file_name . ".xlsx";</span></span><br><span class="line"><span class="comment">       $writer-&gt;save($file_name);*/</span></span><br><span class="line">       <span class="comment">//  第二种直接页面上显示下载</span></span><br><span class="line">       $file_name = $file_name . <span class="string">".xls"</span>;</span><br><span class="line">       header(<span class="string">'Content-Type: application/vnd.ms-excel'</span>);</span><br><span class="line">       header(<span class="string">'Content-Disposition: attachment;filename="'</span> . $file_name . <span class="string">'"'</span>);</span><br><span class="line">       header(<span class="string">'Cache-Control: max-age=0'</span>);</span><br><span class="line">       $writer = IOFactory::createWriter($spreadsheet, <span class="string">'Xls'</span>);</span><br><span class="line">       <span class="comment">//  注意createWriter($spreadsheet, 'Xls') 第二个参数首字母必须大写</span></span><br><span class="line">       $writer-&gt;save(<span class="string">'php://output'</span>);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">getClient</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">       $client = <span class="keyword">new</span> Client();</span><br><span class="line">       <span class="keyword">return</span> $client;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">curlGet</span>(<span class="params">$url</span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">       $ch = curl_init();</span><br><span class="line">       curl_setopt($ch, CURLOPT_URL, $url);</span><br><span class="line">       curl_setopt($ch, CURLOPT_HEADER, <span class="number">0</span>);</span><br><span class="line">       curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="number">1</span>);</span><br><span class="line">       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, <span class="literal">false</span>); <span class="comment">// 这个是重点 请求https。</span></span><br><span class="line">       $data = curl_exec($ch);</span><br><span class="line">       curl_close($ch);</span><br><span class="line">       <span class="keyword">return</span> $data;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-集合的-“when”-方法"><a href="#Laravel-集合的-“when”-方法" class="headerlink" title="Laravel 集合的 “when” 方法"></a>Laravel 集合的 “when” 方法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">$hosts = [</span><br><span class="line">    [<span class="string">'name'</span> =&gt; <span class="string">'Eric Barnes'</span>, <span class="string">'location'</span> =&gt; <span class="string">'USA'</span>, <span class="string">'is_active'</span> =&gt; <span class="number">0</span>],</span><br><span class="line">    [<span class="string">'name'</span> =&gt; <span class="string">'Jack Fruh'</span>, <span class="string">'location'</span> =&gt; <span class="string">'USA'</span>, <span class="string">'is_active'</span> =&gt; <span class="number">0</span>],</span><br><span class="line">    [<span class="string">'name'</span> =&gt; <span class="string">'Jacob Bennett'</span>, <span class="string">'location'</span> =&gt; <span class="string">'USA'</span>, <span class="string">'is_active'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">    [<span class="string">'name'</span> =&gt; <span class="string">'Michael Dyrynda'</span>, <span class="string">'location'</span> =&gt; <span class="string">'AU'</span>, <span class="string">'is_active'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">];</span><br><span class="line">以往，如果在一个查询语句上，还要有进一步的过滤条件，你可能需要这样写：</span><br><span class="line"></span><br><span class="line">$inUsa = collect($hosts)-&gt;where(<span class="string">'location'</span>, <span class="string">'USA'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (request(<span class="string">'retired'</span>)) &#123;</span><br><span class="line">    $inUsa = $inUsa-&gt;filter(<span class="function"><span class="keyword">function</span>(<span class="params">$employee</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> ! $employee[<span class="string">'is_active'</span>];</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line">通过 when 方法，你就可以在一个链式查询中完成所有的筛选过滤：https:<span class="comment">//learnku.com/laravel/t/26331</span></span><br><span class="line"></span><br><span class="line">$inUsa = collect($hosts)</span><br><span class="line">    -&gt;where(<span class="string">'location'</span>, <span class="string">'USA'</span>)</span><br><span class="line">    -&gt;when(request(<span class="string">'retired'</span>), <span class="function"><span class="keyword">function</span>(<span class="params">$collection</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $collection-&gt;reject(<span class="function"><span class="keyword">function</span>(<span class="params">$employee</span>)</span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $employee[<span class="string">'is_active'</span>];</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;);</span><br></pre></td></tr></table></figure>
<h3 id="Jwtauth-自定义认证头信息"><a href="#Jwtauth-自定义认证头信息" class="headerlink" title="Jwtauth 自定义认证头信息"></a>Jwtauth 自定义认证头信息</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">项目使用的 tymon/jwt-auth 包作为 token 的认证，过程中需要迁移项目，因为之前公司的 token 头部使用自定义的，并且他们还修改了包的头信息。就是下面头部信息。</span><br><span class="line">https:<span class="comment">//learnku.com/articles/26976</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AuthHeaders</span> <span class="title">implements</span> <span class="title">ParserContract</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">// 下面这两处就是被修改的</span></span><br><span class="line">    protected $header = <span class="string">'authorization'</span>; </span><br><span class="line"></span><br><span class="line">    protected $prefix = <span class="string">'bearer'</span>;</span><br><span class="line">&#125;</span><br><span class="line">迁移项目过程了，因为拉取了新的包，所以还要去动包的信息，这是极其不合理的行为。所以就在包中尝试找到了更好的解决办法。如果在项目迁移过程中遇到了类似的问题该如何去做呢？这里只提供了我能想到的解决办法。需要在 AppServiceProvider 中加入该方法就可以了。</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">setAuthHeader</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $chain = $<span class="keyword">this</span>-&gt;app[<span class="string">'tymon.jwt.parser'</span>]-&gt;getChain();</span><br><span class="line"></span><br><span class="line">        $chain[<span class="number">0</span>] = $chain[<span class="number">0</span>]-&gt;setHeaderPrefix(<span class="string">'项目的 token 前缀'</span>)-&gt;setHeaderName(<span class="string">'项目的头信息 key'</span>);</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;app[<span class="string">'tymon.jwt.parser'</span>]-&gt;setChain($chain);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="ajax-实现跨域"><a href="#ajax-实现跨域" class="headerlink" title="ajax 实现跨域"></a>ajax 实现跨域</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">$.ajax(&#123;</span><br><span class="line">                <span class="keyword">async</span>: <span class="literal">true</span>,</span><br><span class="line">                url: <span class="string">"http://172.16.112.3/api.php"</span>,</span><br><span class="line">                type: <span class="string">"GET"</span>,</span><br><span class="line">                dataType: <span class="string">"jsonp"</span>, <span class="comment">// 返回的数据类型，设置为JSONP方式</span></span><br><span class="line">                jsonp: <span class="string">'callback'</span>, <span class="comment">//指定一个查询参数名称来覆盖默认的 jsonp 回调参数名 callback</span></span><br><span class="line">                jsonpCallback: <span class="string">'handleResponse'</span>, <span class="comment">//设置回调函数名</span></span><br><span class="line">                success: <span class="function"><span class="keyword">function</span> (<span class="params">response, status, xhr</span>) </span>&#123;</span><br><span class="line">                    <span class="built_in">console</span>.log(<span class="string">'状态为：'</span> + status + <span class="string">',状态是：'</span> + xhr.statusText);</span><br><span class="line">                    <span class="built_in">console</span>.log(response);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;);</span><br><span class="line">原生 js 实现跨域</span><br><span class="line"></span><br><span class="line"> <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="function"><span class="keyword">function</span> <span class="title">jsonp</span>(<span class="params">obj</span>) </span>&#123;</span><br><span class="line">            <span class="comment">//定义一个处理Jsonp返回数据的回调函数</span></span><br><span class="line">            <span class="built_in">window</span>[<span class="string">"callback"</span>] = <span class="function"><span class="keyword">function</span> (<span class="params">object</span>) </span>&#123;</span><br><span class="line">                obj.success(object);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">"script"</span>);</span><br><span class="line">            <span class="comment">//组合请求URL</span></span><br><span class="line">            script.src = obj.url + <span class="string">"?callback=callback"</span>;</span><br><span class="line">            <span class="keyword">for</span> (key <span class="keyword">in</span> obj.data) &#123;</span><br><span class="line">                script.src += <span class="string">"&amp;"</span> + key + <span class="string">"="</span> + obj.data[key];</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//将创建的新节点添加到BOM树上</span></span><br><span class="line">            <span class="built_in">document</span>.getElementsByTagName(<span class="string">"body"</span>)[<span class="number">0</span>].appendChild(script);</span><br><span class="line">        &#125;</span><br><span class="line">        jsonp(&#123;</span><br><span class="line">            url: <span class="string">"http://172.16.112.3/api.php"</span>,</span><br><span class="line">            success: <span class="function"><span class="keyword">function</span> (<span class="params">obj</span>) </span>&#123;</span><br><span class="line">                <span class="built_in">console</span>.log(obj);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="PDO-防止-sql-注入"><a href="#PDO-防止-sql-注入" class="headerlink" title="PDO 防止 sql 注入"></a>PDO 防止 sql 注入</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">setAttribute（） 这一行是强制性的，它会告诉 PDO 禁用模拟预处理语句，并使用 real parepared statements 。这可以确保 SQL 语句和相应的值在传递到 mysql 服务器之前是不会被 PHP 解析的（禁止了所有可能的恶意 SQL 注入攻击）。虽然你可以配置文件中设置 字符集的属性 (charset=utf8)，但是需要格外注意的是，老版本的 PHP（ &lt; <span class="number">5.3</span><span class="number">.6</span>）在 DSN 中是忽略字符参数的。</span><br><span class="line">我们来看一段完整的代码使用实例：https:<span class="comment">//learnku.com/articles/27000#topnav</span></span><br><span class="line"></span><br><span class="line">  $user=$_POST[<span class="string">'user'</span>]; $pass=$_POST[<span class="string">'pass'</span>];</span><br><span class="line">  $dbh = <span class="keyword">new</span> \PDO(<span class="string">"mysql:host=localhost; dbname=zz"</span>, <span class="string">"root"</span>, <span class="string">"root"</span>);</span><br><span class="line">  $dbh-&gt;setAttribute(\PDO::ATTR_EMULATE_PREPARES, <span class="literal">false</span>);</span><br><span class="line">  <span class="comment">//禁用prepared statements的仿真效果</span></span><br><span class="line"><span class="comment">//        $dbh-&gt;exec    ("set names 'utf8'");</span></span><br><span class="line">  $sql=<span class="string">"select * from test where user=? and pass=?"</span>;</span><br><span class="line">  $stmt = $dbh-&gt;prepare($sql);</span><br><span class="line">  $exeres = $stmt-&gt;execute(array($user, $pass));</span><br><span class="line">  <span class="keyword">if</span> ($exeres) &#123;</span><br><span class="line">  <span class="comment">//while条件为真时,输出$row,</span></span><br><span class="line">  <span class="keyword">while</span></span><br><span class="line">  ($row = $stmt-&gt;fetch(\PDO::FETCH_ASSOC))&#123;</span><br><span class="line">  print_r($row);die();</span><br><span class="line"> &#125;  <span class="comment">//失败输出登录失败</span></span><br><span class="line">  print_r(<span class="string">"登录失败"</span>);die();</span><br><span class="line"> &#125;</span><br><span class="line">当调用 prepare () 时，查询语句已经发送给了数据库服务器，此时只有占位符？发送过去，没有用户提交的数据；当调用到 execute () 时，用户提交过来的值才会传送给数据库，他们是分开传送的，两者独立的，SQL 攻击者没有一点机会。</span><br></pre></td></tr></table></figure>
<h3 id="Eloquent-关系中使用-orderBy"><a href="#Eloquent-关系中使用-orderBy" class="headerlink" title="Eloquent 关系中使用 orderBy()"></a>Eloquent 关系中使用 orderBy()</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">productsByName</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(Product::<span class="class"><span class="keyword">class</span>)-&gt;<span class="title">orderBy</span>('<span class="title">name</span>')</span>;</span><br><span class="line">&#125;</span><br><span class="line">$products = Product::whereDate(<span class="string">'created_at'</span>, <span class="string">'2018-01-31'</span>)-&gt;get(); </span><br><span class="line">$products = Product::whereMonth(<span class="string">'created_at'</span>, <span class="string">'12'</span>)-&gt;get(); </span><br><span class="line">$products = Product::whereDay(<span class="string">'created_at'</span>, <span class="string">'31'</span>)-&gt;get(); </span><br><span class="line">$products = Product::whereYear(<span class="string">'created_at'</span>, date(<span class="string">'Y'</span>))-&gt;get(); </span><br><span class="line">$products = Product::whereTime(<span class="string">'created_at'</span>, <span class="string">'='</span>, <span class="string">'14:13:58'</span>)-&gt;get();</span><br><span class="line">Route::group([<span class="string">'prefix'</span> =&gt; <span class="string">'account'</span>, <span class="string">'as'</span> =&gt; <span class="string">'account.'</span>], <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    Route::get(<span class="string">'login'</span>, <span class="string">'AccountController@login'</span>);     </span><br><span class="line">    Route::get(<span class="string">'register'</span>, <span class="string">'AccountController@register'</span>);</span><br><span class="line">    Route::group([<span class="string">'middleware'</span> =&gt; <span class="string">'auth'</span>], <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;         </span><br><span class="line">        Route::get(<span class="string">'edit'</span>, <span class="string">'AccountController@edit'</span>);     </span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br><span class="line">恢复多个软删除https:<span class="comment">//learnku.com/articles/26673 </span></span><br><span class="line"></span><br><span class="line">如果记录使用了软删除，那么你就可以一次恢复多条软删除记录。</span><br><span class="line"></span><br><span class="line">Post::withTrashed()-&gt;where(<span class="string">'author_id'</span>, <span class="number">1</span>)-&gt;restore();</span><br><span class="line"><span class="comment">// Author -&gt; hasMany(Book::class) </span></span><br><span class="line">$authors = Author::has(<span class="string">'books'</span>, <span class="string">'&gt;'</span>, <span class="number">5</span>)-&gt;get();</span><br><span class="line">$users = User::all([<span class="string">'id'</span>, <span class="string">'name'</span>, <span class="string">'email'</span>]);</span><br><span class="line">在执行 Eloqument 查询后，你可以使用 map() 来修改行。</span><br><span class="line"></span><br><span class="line">$users = User::where(<span class="string">'role_id'</span>, <span class="number">1</span>)-&gt;get()-&gt;map(<span class="function"><span class="keyword">function</span> (<span class="params">User $user</span>) </span>&#123;</span><br><span class="line">    $user-&gt;some_column = some_function($user);</span><br><span class="line">    <span class="keyword">return</span> $user;</span><br><span class="line">&#125;);</span><br><span class="line">当一个关系被调用时，如果它不存在，则会出现致命的错误，例如 $post-&gt;user-&gt;name ，可以使用 withDefault() 来避免。</span><br><span class="line"></span><br><span class="line"><span class="comment">/** 获取文章作者 */</span> </span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">user</span>(<span class="params"></span>) </span></span><br><span class="line"><span class="function"></span>&#123;     </span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;belongsTo(<span class="string">'App\User'</span>)-&gt;withDefault(); </span><br><span class="line">&#125;</span><br><span class="line">$users = App\Book::<span class="keyword">with</span>(<span class="string">'author:id,name'</span>)-&gt;get();</span><br></pre></td></tr></table></figure>
<h3 id="开闭原则"><a href="#开闭原则" class="headerlink" title="开闭原则"></a>开闭原则</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line">　Controller 类，只接受用户输入，返回输出，不需要具体处理背后的事情。当需要表单验证的时候，注入相应的 Request 类。当需要数据操作时，注入相应的 Repository 或 Service 或 Factory。</span><br><span class="line"></span><br><span class="line">　　Model 类，将修改器，访问器定义在 trait 然后 use 进来。数据操作逻辑分离成 Repository。</span><br><span class="line"></span><br><span class="line">　　Helper.php 全局函数，将同类型的 helper 分离成单独的文件，使其高内聚。然后在 Helper.php 中引入</span><br><span class="line">所有具体的类都需要实现 Interface 中定义的方法。</span><br><span class="line">在 Factory 中实例化具体的类。</span><br><span class="line">在 Controller 中使用 Factory。</span><br><span class="line"></span><br><span class="line">首先创建一个 PaymentInterface，任何支付方式都必须实现此接口中定义的方法。</span><br><span class="line"></span><br><span class="line">interface PaymentInterface &#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">pay</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function">接着，创建两个实现 <span class="title">PaymentInterface</span> 的具体类。</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">Alipay</span> <span class="title">implements</span> <span class="title">PaymentInterface</span> </span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">pay</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 支付宝支付具体代码</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Wechat</span> <span class="title">implements</span> <span class="title">PaymentInterface</span> </span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">pay</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 微信支付具体代码</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">然后，创建一个支付工厂，此工厂用来实例化具体的支付类</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PeymentFactory</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">init</span>(<span class="params">$payType</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">switch</span> ($payType) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'Alipay'</span>:</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">new</span> Alipay();</span><br><span class="line">            <span class="keyword">case</span> <span class="string">'Wechat'</span>:</span><br><span class="line">                <span class="keyword">return</span> <span class="keyword">new</span> Wechat();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">'未知支付方式'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">最后，在控制器中注入此 Factory</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">pay</span> (<span class="params">Request $request, PaymentFactory $paymentFactory</span>) </span>&#123;</span><br><span class="line">    $payment = $paymentFactory-&gt;init($request-&gt;payType);</span><br><span class="line">    $payment-&gt;pay();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="内存占用高，速度就会提高"><a href="#内存占用高，速度就会提高" class="headerlink" title="内存占用高，速度就会提高"></a>内存占用高，速度就会提高</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">a</span></span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'class a __construct'</span>.<span class="string">"\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">a1</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'a1'</span>.<span class="string">"\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">a2</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'a2'</span>.<span class="string">"\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">a3</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'a3'</span>.<span class="string">"\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/laravel/t/27033</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">b</span></span>&#123;</span><br><span class="line">    protected $cache;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        echo <span class="string">'class b __construct'</span>.<span class="string">"\n"</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">b1</span>(<span class="params">$tags=<span class="string">'tags'</span></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> a();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">b2</span>(<span class="params">$tags=<span class="string">'tags'</span></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">static</span> $cache=[];</span><br><span class="line">        <span class="keyword">if</span>(isset($cache[$tags])) <span class="keyword">return</span> $cache[$tags];</span><br><span class="line">        <span class="keyword">return</span> $cache[$tags]=<span class="keyword">new</span> a();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">b3</span>(<span class="params">$tags=<span class="string">'tags'</span></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(isset($<span class="keyword">this</span>-&gt;cache[$tags])) <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;cache[$tags];</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;cache[$tags]=<span class="keyword">new</span> a();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*内存计算*/</span></span><br><span class="line">$start = memory_get_usage();</span><br><span class="line">$b=<span class="keyword">new</span> b();</span><br><span class="line">$b-&gt;b1()-&gt;a1();</span><br><span class="line">$b-&gt;b1()-&gt;a2();</span><br><span class="line">$b-&gt;b1()-&gt;a3();</span><br><span class="line"><span class="comment">/*内存计算*/</span></span><br><span class="line">$end = memory_get_usage();</span><br><span class="line">echo ($end-$start).<span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*内存计算*/</span></span><br><span class="line">$start = memory_get_usage();</span><br><span class="line">$b=<span class="keyword">new</span> b();</span><br><span class="line">$b-&gt;b2()-&gt;a1();</span><br><span class="line">$b-&gt;b2()-&gt;a2();</span><br><span class="line">$b-&gt;b2()-&gt;a3();</span><br><span class="line"><span class="comment">/*内存计算*/</span></span><br><span class="line">$end = memory_get_usage();</span><br><span class="line">echo ($end-$start).<span class="string">"\n"</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*内存计算*/</span></span><br><span class="line">$start = memory_get_usage();</span><br><span class="line">$b=<span class="keyword">new</span> b();</span><br><span class="line">$b-&gt;b3()-&gt;a1();</span><br><span class="line">$b-&gt;b3()-&gt;a2();</span><br><span class="line">$b-&gt;b3()-&gt;a3();</span><br><span class="line"><span class="comment">/*内存计算*/</span></span><br><span class="line">$end = memory_get_usage();</span><br><span class="line">echo ($end-$start).<span class="string">"\n"</span>;</span><br><span class="line">内存占用高，速度就会提高，反之，就速度就会降低，上面那个 b1 和 b2 后面应该加一个 unset ($b), 内存计算才准确吧</span><br><span class="line">这应该就是连接数据库的时候为什么推荐使用短链接而不是长链接的原因吧，长连接占用内存高，资源多吧，不即时释放</span><br><span class="line"></span><br><span class="line">调用第二个 b1 的时候，第一个 b1 创建出来的 a 就被销毁了啊，就不占内存了啊。</span><br><span class="line">这题还有一个目的，在同一个进程下想减少 <span class="keyword">new</span> 的次数，结果画蛇添足了</span><br></pre></td></tr></table></figure>
<h3 id="关联模型字段取别名查询不出数据"><a href="#关联模型字段取别名查询不出数据" class="headerlink" title="关联模型字段取别名查询不出数据"></a>关联模型字段取别名查询不出数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/articles/22489#topnav</span></span><br><span class="line">laravel 自带的 ORM 是个神器，针对这种有关系的数据，完全可以使用关系模型，既简单又实用，由于这里只有一个数据表，关系也存在于同一张表，所以可以直接使用自关联，将两个关系定义在同一个 Model 里面：</span><br><span class="line"></span><br><span class="line">    ......</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">parent</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasOne($<span class="keyword">this</span>, <span class="string">'id'</span>, <span class="string">'parent_id'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">children</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany($<span class="keyword">this</span>, <span class="string">'parent_id'</span>, <span class="string">'id'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">关系定义里面的参数也可以这样写：</span><br><span class="line"></span><br><span class="line">    ......</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">parent</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasOne(get_class($<span class="keyword">this</span>), $<span class="keyword">this</span>-&gt;getKeyName(), <span class="string">'parent_id'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">children</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;hasMany(get_class($<span class="keyword">this</span>), <span class="string">'parent_id'</span>, $<span class="keyword">this</span>-&gt;getKeyName());</span><br><span class="line">    &#125;</span><br><span class="line">查询包含子菜单的数据</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> Admin_menu::<span class="keyword">with</span>([<span class="string">'children'</span> =&gt; <span class="function"><span class="keyword">function</span>(<span class="params">$query</span>)</span>&#123;</span><br><span class="line">                $query-&gt;select(<span class="string">'id'</span>, <span class="string">'title'</span>, <span class="string">'parent_id'</span>);</span><br><span class="line">            &#125;])</span><br><span class="line">            -&gt;select(<span class="string">'id'</span>, <span class="string">'title'</span>, <span class="string">'parent_id'</span>)</span><br><span class="line">            -&gt;get();</span><br><span class="line">where <span class="keyword">in</span> （array）这里的 array 是依赖主键的名称的，在关联查询的时候，已经定义了 id = [<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6.</span>..]，但是我们最后给 id 取了别名，变成 MaindId，所以找不到名为 id 的数组。</span><br><span class="line">如果真是这样，我们试着再给它加上 id，让它能够找到名为 id 的数组</span><br><span class="line">    \DB::connection()-&gt;enableQueryLog(); <span class="comment">// 开启查询日志</span></span><br><span class="line">    $menus =  Admin_menu::<span class="keyword">with</span>([<span class="string">'children'</span> =&gt; <span class="function"><span class="keyword">function</span>(<span class="params">$query</span>)</span>&#123;</span><br><span class="line">        $query-&gt;select(<span class="string">'id'</span>, <span class="string">'title'</span>, <span class="string">'parent_id'</span>);</span><br><span class="line">    &#125;])</span><br><span class="line">    -&gt;select(<span class="string">'id'</span>, <span class="string">'id as MainId'</span>, <span class="string">'title'</span>, <span class="string">'parent_id'</span>)</span><br><span class="line">    -&gt;get();</span><br><span class="line">    foreach (\DB::getQueryLog() <span class="keyword">as</span> $sql) &#123;</span><br><span class="line">        dump($sql[<span class="string">'query'</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">    依赖关联主键 localKey 的查询，不能缺少相应的字段，也就是说 select 应该包含对应 localKey，如果要取别名应该额外添加，形如：</span><br><span class="line">    </span><br><span class="line">    select(<span class="string">'id'</span>, <span class="string">'id as MainId'</span>, <span class="string">'title'</span>, <span class="string">'parent_id'</span>, <span class="string">'parent_id as extraId'</span>)</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">test</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        $menus = Admin_menu::<span class="keyword">with</span>([<span class="string">'parent'</span> =&gt; <span class="function"><span class="keyword">function</span>(<span class="params">$query</span>)</span>&#123;</span><br><span class="line">                $query-&gt;select(<span class="string">'id'</span>, <span class="string">'title'</span>, <span class="string">'parent_id'</span>);</span><br><span class="line">            &#125;])</span><br><span class="line">            -&gt;select(<span class="string">'id'</span>, <span class="string">'title'</span>, <span class="string">'parent_id'</span>)</span><br><span class="line">            -&gt;get();</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;transformer($menus);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">transformer</span>(<span class="params">$items</span>) </span>&#123;</span><br><span class="line">        $data = [];</span><br><span class="line">        foreach ($items ?? [] <span class="keyword">as</span> $item) &#123;</span><br><span class="line">            $data[] = [</span><br><span class="line">                <span class="string">'mainId'</span> =&gt; $item-&gt;id,</span><br><span class="line">                <span class="string">'title'</span> =&gt; $item-&gt;title,</span><br><span class="line">                <span class="string">'parent_id'</span> =&gt; $item-&gt;parent_id,</span><br><span class="line">                <span class="string">'children'</span> =&gt; $item-&gt;children,</span><br><span class="line">            ];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $data;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="敏感词过滤算法"><a href="#敏感词过滤算法" class="headerlink" title="敏感词过滤算法"></a>敏感词过滤算法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//</span></span><br><span class="line">https:<span class="comment">//juejin.im/post/5cb56372f265da037d4fa133</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TreeMap</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $data;  <span class="comment">// 节点字符</span></span><br><span class="line">    public $children = [];  <span class="comment">// 存放子节点引用（因为有任意个子节点，所以靠数组来存储）</span></span><br><span class="line">    public $isEndingChar = <span class="literal">false</span>;  <span class="comment">// 是否是字符串结束字符</span></span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$data</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;data = $data;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">TrieTree</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 敏感词数组</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @var array</span></span><br><span class="line"><span class="comment">     * @author qpf</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public $trieTreeMap = array();</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;trieTreeMap = <span class="keyword">new</span> TreeMap(<span class="string">'/'</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 获取敏感词Map</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @return array</span></span><br><span class="line"><span class="comment">     * @author qpf</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getTreeMap</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;trieTreeMap;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 添加敏感词</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param array $txtWords</span></span><br><span class="line"><span class="comment">     * @author qpf</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">addWords</span>(<span class="params">array $wordsList</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        foreach ($wordsList <span class="keyword">as</span> $words) &#123;</span><br><span class="line">            $trieTreeMap = $<span class="keyword">this</span>-&gt;trieTreeMap;</span><br><span class="line">            $len = mb_strlen($words);</span><br><span class="line">            <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; $len; $i++) &#123;</span><br><span class="line">                $word = mb_substr($words, $i, <span class="number">1</span>);</span><br><span class="line">                <span class="keyword">if</span>(!isset($trieTreeMap-&gt;children[$word]))&#123;</span><br><span class="line">                    $newNode = <span class="keyword">new</span> TreeMap($word);</span><br><span class="line">                    $trieTreeMap-&gt;children[$word] = $newNode;</span><br><span class="line">                &#125;</span><br><span class="line">                $trieTreeMap = $trieTreeMap-&gt;children[$word];</span><br><span class="line">            &#125;</span><br><span class="line">            $trieTreeMap-&gt;isEndingChar = <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 查找对应敏感词</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param string $txt</span></span><br><span class="line"><span class="comment">     * @return array</span></span><br><span class="line"><span class="comment">     * @author qpf</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">search</span>(<span class="params">$txt</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $wordsList = array();</span><br><span class="line">        $txtLength = mb_strlen($txt);</span><br><span class="line">        <span class="keyword">for</span> ($i = <span class="number">0</span>; $i &lt; $txtLength; $i++) &#123;</span><br><span class="line">            $wordLength = $<span class="keyword">this</span>-&gt;checkWord($txt, $i, $txtLength);</span><br><span class="line">            <span class="keyword">if</span>($wordLength &gt; <span class="number">0</span>) &#123;</span><br><span class="line">                echo $wordLength;</span><br><span class="line">                $words = mb_substr($txt, $i, $wordLength);</span><br><span class="line">                $wordsList[] = $words;</span><br><span class="line">                $i += $wordLength - <span class="number">1</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $wordsList;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 敏感词检测</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">     * @param $txt</span></span><br><span class="line"><span class="comment">     * @param $beginIndex</span></span><br><span class="line"><span class="comment">     * @param $length</span></span><br><span class="line"><span class="comment">     * @return int</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">checkWord</span>(<span class="params">$txt, $beginIndex, $length</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $flag = <span class="literal">false</span>;</span><br><span class="line">        $wordLength = <span class="number">0</span>;</span><br><span class="line">        $trieTree = $<span class="keyword">this</span>-&gt;trieTreeMap; <span class="comment">//获取敏感词树</span></span><br><span class="line">        <span class="keyword">for</span> ($i = $beginIndex; $i &lt; $length; $i++) &#123;</span><br><span class="line">            $word = mb_substr($txt, $i, <span class="number">1</span>); <span class="comment">//检验单个字</span></span><br><span class="line">            <span class="keyword">if</span> (!isset($trieTree-&gt;children[$word])) &#123; <span class="comment">//如果树中不存在，结束        </span></span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">//如果存在</span></span><br><span class="line">            $wordLength++; </span><br><span class="line">            $trieTree = $trieTree-&gt;children[$word];</span><br><span class="line">            <span class="keyword">if</span> ($trieTree-&gt;isEndingChar === <span class="literal">true</span>) &#123;  </span><br><span class="line">                $flag = <span class="literal">true</span>;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>($beginIndex &gt; <span class="number">0</span>) &#123;</span><br><span class="line">            $flag || $wordLength = <span class="number">0</span>; <span class="comment">//如果$flag == false  赋值$wordLenth为0</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $wordLength;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$data = [<span class="string">'白粉'</span>, <span class="string">'白粉人'</span>, <span class="string">'白粉人嫩'</span>,<span class="string">'不该大'</span>];</span><br><span class="line">$wordObj = <span class="keyword">new</span> TrieTree();</span><br><span class="line">$wordObj-&gt;addWords($data);</span><br><span class="line"></span><br><span class="line">$txt = <span class="string">"白粉啊,白粉人，我不该大啊"</span>;</span><br><span class="line">$words = $wordObj-&gt;search($txt);</span><br><span class="line">var_dump($words);die;</span><br></pre></td></tr></table></figure>
<h3 id="curl-https"><a href="#curl-https" class="headerlink" title="curl https"></a>curl https</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">curl请求https，需要设置ipv4访问，ipv6的话会导致解析不了域名，php代码如下：</span><br><span class="line"><span class="comment">//请求https需要用ipv4</span></span><br><span class="line">$curl-&gt;setOption(CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);</span><br><span class="line"><span class="comment">/** </span></span><br><span class="line"><span class="comment"> * curl POST </span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> * @param   string  url </span></span><br><span class="line"><span class="comment"> * @param   array   数据 </span></span><br><span class="line"><span class="comment"> * @param   int     请求超时时间 </span></span><br><span class="line"><span class="comment"> * @param   bool    HTTPS时是否进行严格认证 </span></span><br><span class="line"><span class="comment"> * @return  string </span></span><br><span class="line"><span class="comment"> */</span>  </span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">curlPost</span>(<span class="params">$url, $data = array(</span>), <span class="title">$timeout</span> = 30, <span class="title">$CA</span> = <span class="title">true</span>)</span>&#123;    </span><br><span class="line">  </span><br><span class="line">    $cacert = getcwd() . <span class="string">'/cacert.pem'</span>; <span class="comment">//CA根证书https://segmentfault.com/a/1190000005856334  </span></span><br><span class="line">    $SSL = substr($url, <span class="number">0</span>, <span class="number">8</span>) == <span class="string">"https://"</span> ? <span class="literal">true</span> : <span class="literal">false</span>;  </span><br><span class="line">      </span><br><span class="line">    $ch = curl_init();  </span><br><span class="line">    curl_setopt($ch, CURLOPT_URL, $url);  </span><br><span class="line">    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  </span><br><span class="line">    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout<span class="number">-2</span>);  </span><br><span class="line">    <span class="keyword">if</span> ($SSL &amp;&amp; $CA) &#123;  </span><br><span class="line">        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, <span class="literal">true</span>);   <span class="comment">// 只信任CA颁布的证书  </span></span><br><span class="line">        curl_setopt($ch, CURLOPT_CAINFO, $cacert); <span class="comment">// CA根证书（用来验证的网站证书是否是CA颁布）  </span></span><br><span class="line">        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, <span class="number">2</span>); <span class="comment">// 检查证书中是否设置域名，并且是否与提供的主机名匹配  </span></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> ($SSL &amp;&amp; !$CA) &#123;  </span><br><span class="line">        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, <span class="literal">false</span>); <span class="comment">// 信任任何证书  </span></span><br><span class="line">        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, <span class="number">1</span>); <span class="comment">// 检查证书中是否设置域名  </span></span><br><span class="line">    &#125;  </span><br><span class="line">    curl_setopt($ch, CURLOPT_RETURNTRANSFER, <span class="literal">true</span>);  </span><br><span class="line">    curl_setopt($ch, CURLOPT_HTTPHEADER, array(<span class="string">'Expect:'</span>)); <span class="comment">//避免data数据过长问题  </span></span><br><span class="line">    curl_setopt($ch, CURLOPT_POST, <span class="literal">true</span>);  </span><br><span class="line">    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  </span><br><span class="line">    <span class="comment">//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //data with URLEncode  </span></span><br><span class="line">  </span><br><span class="line">    $ret = curl_exec($ch);  </span><br><span class="line">    <span class="comment">//var_dump(curl_error($ch));  //查看报错信息  </span></span><br><span class="line">  </span><br><span class="line">    curl_close($ch);  </span><br><span class="line">    <span class="keyword">return</span> $ret;    </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-Excel3-0"><a href="#Laravel-Excel3-0" class="headerlink" title="Laravel-Excel3.0"></a>Laravel-Excel3.0</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> maatwebsite/excel</span><br><span class="line">app/app 注册服务及门面：</span><br><span class="line"></span><br><span class="line"><span class="string">'providers'</span> =&gt; [   </span><br><span class="line">    Maatwebsite\Excel\ExcelServiceProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">]</span><br><span class="line"><span class="string">'aliases'</span> =&gt; [ </span><br><span class="line">    <span class="string">'Excel'</span> =&gt; Maatwebsite\Excel\Facades\Excel::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">]</span><br><span class="line">php artisan vendor:publish</span><br><span class="line"></span><br><span class="line">创建 app/exports/<span class="keyword">export</span>.php</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">namespace App\Exports;</span><br><span class="line"></span><br><span class="line">use Maatwebsite\Excel\Concerns\FromCollection;</span><br><span class="line">use Maatwebsite\Excel\Concerns\WithHeadings; <span class="comment">//设置标题</span></span><br><span class="line">use Maatwebsite\Excel\Concerns\ShouldAutoSize; <span class="comment">//自动单元格尺寸</span></span><br><span class="line">use PhpOffice\PhpSpreadsheet\Style\NumberFormat; <span class="comment">//设置单元格数据格式</span></span><br><span class="line">use Maatwebsite\Excel\Concerns\WithColumnFormatting; <span class="comment">//设置列格式</span></span><br><span class="line">use Maatwebsite\Excel\Concerns\WithStrictNullComparison; <span class="comment">//为空时零填充</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">InvoicesExport</span> <span class="title">implements</span> <span class="title">FromCollection</span>,<span class="title">WithHeadings</span>,<span class="title">WithStrictNullComparison</span>,<span class="title">WithColumnFormatting</span>,<span class="title">ShouldAutoSize</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $data;</span><br><span class="line"></span><br><span class="line">    protected $header;</span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">    * Excel类的构造函数</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$data, $header</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;data = $data;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;header = $header;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//导出数据逻辑</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">collection</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;data;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//首行标题</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">headings</span>(<span class="params"></span>): <span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;header;</span><br><span class="line">    &#125;</span><br><span class="line">      <span class="comment">//设置列格式</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">columnFormats</span>(<span class="params"></span>): <span class="title">array</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> [</span><br><span class="line">            <span class="comment">//'E' =&gt; NumberFormat::FORMAT_DATE_XLSX14,</span></span><br><span class="line">        ];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">控制器中使用：</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">export</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    $data = Post::get();</span><br><span class="line">    $header = [ <span class="string">'ID'</span>,</span><br><span class="line">        <span class="string">'姓名'</span>,</span><br><span class="line">        <span class="string">'年龄'</span>,</span><br><span class="line">        <span class="string">'性别'</span>,</span><br><span class="line">        <span class="string">'创建时间'</span>,</span><br><span class="line">        <span class="string">'修改时间'</span>];</span><br><span class="line">    <span class="keyword">return</span> \Excel::download(<span class="keyword">new</span> InvoicesExport($data, $header), <span class="string">"测试导出.xls"</span>);</span><br><span class="line">&#125;</span><br><span class="line">创建 app/Exports/Import.php</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">namespace App\Exports;</span><br><span class="line"></span><br><span class="line">use Maatwebsite\Excel\Concerns\ToArray;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Import</span> <span class="title">implements</span> <span class="title">ToArray</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">  <span class="comment">//重新父类实现</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">array</span>(<span class="params">array $array</span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $array;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">控制器使用：https:<span class="comment">//learnku.com/articles/26940</span></span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">import</span>(<span class="params">Request $request</span>)</span>&#123;</span><br><span class="line">    $file = $request-&gt;file(<span class="string">'file'</span>);</span><br><span class="line">    $data = \Excel::toArray(<span class="keyword">new</span> Import(), $file);</span><br><span class="line">    dd($data);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="数据填充功能生成中文测试数据"><a href="#数据填充功能生成中文测试数据" class="headerlink" title="数据填充功能生成中文测试数据"></a>数据填充功能生成中文测试数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">$factory-&gt;define(App\Product::<span class="class"><span class="keyword">class</span>, <span class="title">function</span> (<span class="title">Faker</span>\<span class="title">Generator</span> $<span class="title">faker</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> [</span><br><span class="line">        <span class="string">'user_id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">        <span class="string">'name'</span> =&gt; $faker-&gt;name,</span><br><span class="line">        <span class="string">'mobile'</span> =&gt; $faker-&gt;phoneNumber,</span><br><span class="line">        <span class="string">'province'</span> =&gt; $faker-&gt;state,</span><br><span class="line">        <span class="string">'city'</span> =&gt; $faker-&gt;city,</span><br><span class="line">        <span class="string">'area'</span> =&gt; $faker-&gt;area,</span><br><span class="line">        <span class="string">'address'</span> =&gt; $faker-&gt;streetAddress,</span><br><span class="line">        <span class="string">'postcode'</span> =&gt; $faker-&gt;postcode,</span><br><span class="line">    ];</span><br><span class="line">&#125;);</span><br><span class="line">$factory-&gt;define(App\Address::<span class="class"><span class="keyword">class</span>, <span class="title">function</span> () </span>&#123;</span><br><span class="line">    $faker = Faker\Factory::create(<span class="string">'zh_CN'</span>);<span class="comment">//在 config\app.php 文件中加入 faker_locale =&gt; 'zh_CN' 就可以实现了</span></span><br><span class="line"><span class="comment">//https://tianyong90.com/2019/03/10/shi-yong-laravel-shu-ju-tian-chong-gong-neng-sheng-cheng-zhong-wen-ce-shi-shu-ju/</span></span><br><span class="line">    <span class="keyword">return</span> [</span><br><span class="line">        <span class="string">'user_id'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">        <span class="string">'name'</span> =&gt; $faker-&gt;name,</span><br><span class="line">        <span class="string">'mobile'</span> =&gt; $faker-&gt;phoneNumber,</span><br><span class="line">        <span class="string">'province'</span> =&gt; $faker-&gt;state,</span><br><span class="line">        <span class="string">'city'</span> =&gt; $faker-&gt;city,</span><br><span class="line">        <span class="string">'area'</span> =&gt; $faker-&gt;area,</span><br><span class="line">        <span class="string">'address'</span> =&gt; $faker-&gt;streetAddress,</span><br><span class="line">        <span class="string">'postcode'</span> =&gt; $faker-&gt;postcode,</span><br><span class="line">    ];</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="intervention-image-坑"><a href="#intervention-image-坑" class="headerlink" title="intervention/image 坑"></a>intervention/image 坑</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 记录开始时间</span></span><br><span class="line">$startTimestamp = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">$url = <span class="string">'http://wx.qlogo.cn/mmopen/XxT9TiaJ1ibf06TNRCMjQADS4opDHvQLguLZHpqkRlvuJYZicvJW4iaOalPsKIs0kpZ3F6864ZzibyObYiaucUQSrdp4pFTNDyIpxw/0'</span>;</span><br><span class="line"></span><br><span class="line">$avatar = \Image::make($url);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 记录结束时间</span></span><br><span class="line">$endTimestamp = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">info($startTimestamp);</span><br><span class="line">info($endTimestamp);</span><br><span class="line">info($endTimestamp - $startTimestamp);</span><br><span class="line">$startTimestamp = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">$client = <span class="keyword">new</span> \GuzzleHttp\Client();</span><br><span class="line"></span><br><span class="line">$url = <span class="string">'http://wx.qlogo.cn/mmopen/XxT9TiaJ1ibf06TNRCMjQADS4opDHvQLguLZHpqkRlvuJYZicvJW4iaOalPsKIs0kpZ3F6864ZzibyObYiaucUQSrdp4pFTNDyIpxw/0'</span>;</span><br><span class="line"></span><br><span class="line">$avatarResponse = $client-&gt;get($url);</span><br><span class="line"></span><br><span class="line">$avatar = \Image::make($avatarResponse-&gt;getBody()-&gt;getContents());</span><br><span class="line"></span><br><span class="line">$endTimestamp = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">info($startTimestamp);</span><br><span class="line">info($endTimestamp);</span><br><span class="line">info($endTimestamp - $startTimestamp);</span><br><span class="line">先使用 GuzzleHttp 获取头像，再使用 Image::make($data) 创建头像。</span><br></pre></td></tr></table></figure>
<h3 id="puppeteer-采集异步加载的网页内容"><a href="#puppeteer-采集异步加载的网页内容" class="headerlink" title="puppeteer 采集异步加载的网页内容"></a>puppeteer 采集异步加载的网页内容</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">$ composer <span class="built_in">require</span> spatie/browsershot</span><br><span class="line">$ npm i puppeteer --save</span><br><span class="line">https:<span class="comment">//tianyong90.com/2019/03/10/laravel-zhong-shi-yong-puppeteer-cai-ji-yi-bu-jia-zai-de-wang-ye-nei-rong/#安装</span></span><br><span class="line">use Spatie\Browsershot\Browsershot;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getBodyHtml</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $newsUrl = <span class="string">'https://m.toutiao.com/i6546884151050502660/'</span>;</span><br><span class="line">    </span><br><span class="line">    $html = Browsershot::url($newsUrl)</span><br><span class="line">        -&gt;windowSize(<span class="number">480</span>, <span class="number">800</span>)</span><br><span class="line">        -&gt;userAgent(<span class="string">'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36'</span>)</span><br><span class="line">        -&gt;mobile()</span><br><span class="line">        -&gt;touch()</span><br><span class="line">        -&gt;bodyHtml();</span><br><span class="line"></span><br><span class="line">    \Log::info($html);</span><br><span class="line">&#125;</span><br><span class="line">use Spatie\Browsershot\Browsershot;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">getBodyHtml</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $newsUrl = <span class="string">'https://m.toutiao.com/i6546884151050502660/'</span>;</span><br><span class="line">    </span><br><span class="line">    Browsershot::url($newsUrl)</span><br><span class="line">        -&gt;windowSize(<span class="number">480</span>, <span class="number">800</span>)</span><br><span class="line">        -&gt;userAgent(<span class="string">'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Mobile Safari/537.36'</span>)</span><br><span class="line">        -&gt;mobile()</span><br><span class="line">        -&gt;touch()</span><br><span class="line">        -&gt;setDelay(<span class="number">1000</span>)</span><br><span class="line">        -&gt;save(public_path(<span class="string">'images/toutiao.jpg'</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="PHP时区"><a href="#PHP时区" class="headerlink" title="PHP时区"></a>PHP时区</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>，北京英国标准时间时差是<span class="number">8</span>小时。</span><br><span class="line"><span class="number">2</span>，英国伦敦使用夏令时（夏时制）时时差是<span class="number">7</span>小时。</span><br><span class="line">北京时间为东八区的区时UTC+<span class="number">8</span>，英国伦敦为零时区的区时UTC+<span class="number">0</span>，两者时差为<span class="number">8</span>小时；</span><br><span class="line">当英国为夏时制时（提前一小时），时区变为UTC+<span class="number">1</span>，两者时差为<span class="number">7</span>小时。</span><br><span class="line">英国夏令时的区间：从<span class="number">3</span>月最后一个星期日到<span class="number">10</span>月最后一个星期日。</span><br><span class="line">参考：http:<span class="comment">//baike.baidu.com/view/37429.htm</span></span><br><span class="line">时差的计算方法：两个时区标准时间（即时区数）相减就是时差，时区的数值大的时间早。比如中国是东八区(+<span class="number">8</span>)，美国东部是西五区(<span class="number">-5</span>)，两地的时差是<span class="number">13</span>小时，北京比纽约要早<span class="number">13</span>个小时；如果是美国实行夏令时的时期，相差<span class="number">12</span>小时。</span><br><span class="line">英国与北京时间的时差是<span class="number">8</span>个小时，但其中英国属于夏令时国家，如果是夏令时（每年<span class="number">3</span>月最后一个星期日至<span class="number">10</span>月最后一个星期日）期间，中国和英国的时差是<span class="number">7</span>个小时。</span><br><span class="line"></span><br><span class="line"><span class="number">1884</span>年在华盛顿召开的一次国际子午线会议上，规定将全球划分为<span class="number">24</span>个时区（东、西各<span class="number">12</span>个时区），且同时规定了英国为为本初子午线，即零度经线。东<span class="number">1</span><span class="number">-12</span>区，西<span class="number">1</span><span class="number">-12</span>区，每个时区横跨经度<span class="number">15</span>度，时间正好是<span class="number">1</span>小时。最后的东、西第<span class="number">12</span>区各跨经度<span class="number">7.5</span>度，以东、西经<span class="number">180</span>度为界。</span><br><span class="line">中国首都北属于东八区，所以对比英国的零度经线，两者之间相差了<span class="number">8</span>个小时。但因为英国实行夏令时（每年<span class="number">3</span>月最后一个星期日至<span class="number">10</span>月最后一个星期日），则英国在天亮早的夏季人为将时间调快一小时可以使人早起早睡，减少照明量。那么英国与北京时间之间时差是<span class="number">7</span>个小时。</span><br><span class="line"></span><br><span class="line"><span class="comment">//获取美国冬夏时令 //判断当前是否为夏令时，为真返回1，否则为0</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getAmericanSeason</span>(<span class="params">$market</span>)</span>&#123;</span><br><span class="line">	$localzone = date(<span class="string">"e"</span>);</span><br><span class="line">	<span class="comment">//date_default_timezone_set('US/Pacific-New');</span></span><br><span class="line">	$timezone = in_array($market, array(<span class="string">'LME'</span>, <span class="string">'LIFFE'</span>)) ? <span class="string">'Europe/London'</span> : <span class="string">'US/Pacific-New'</span>;</span><br><span class="line">	$season = date(<span class="string">"I"</span>);</span><br><span class="line">	date_default_timezone_set($localzone);</span><br><span class="line">	<span class="keyword">return</span> $season;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//判断美国那个时间段是否为夏令时https://gist.github.com/flowerains </span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">is_dst</span>(<span class="params">$timestamp</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $timezone = date(<span class="string">'e'</span>); <span class="comment">//获取当前使用的时区</span></span><br><span class="line">    date_default_timezone_set(<span class="string">'US/Pacific-New'</span>); <span class="comment">//强制设置时区</span></span><br><span class="line">    $dst = date(<span class="string">'I'</span>,$timestamp); <span class="comment">//判断是否夏令时</span></span><br><span class="line">    date_default_timezone_set($timezone); <span class="comment">//还原时区</span></span><br><span class="line">    <span class="keyword">return</span> $dst; <span class="comment">//返回结果</span></span><br><span class="line">&#125;</span><br><span class="line">print gmdate(<span class="string">"Y-m-d\TH:i:s\Z"</span>);</span><br><span class="line">$date_utc = <span class="keyword">new</span> \DateTime(<span class="string">"now"</span>, <span class="keyword">new</span> \DateTimeZone(<span class="string">"UTC"</span>));</span><br><span class="line">https:<span class="comment">//stackoverflow.com/questions/8655515/get-utc-time-in-php/18808833</span></span><br><span class="line">echo $date_utc-&gt;format(\DateTime::RFC850); # Saturday, 18-Apr-15 03:23:46 UTC</span><br><span class="line">echo gmdate (<span class="string">"l d F Y H:i:s"</span>).<span class="string">" GMT"</span>; <span class="comment">//输出: Wednesday 09 April 2014 03:53:36 GMT</span></span><br><span class="line">GMT格林威治时间和本地的时间是有时差的, 我们知道php指定时区后用date() 函数获取的是本地时间, 如果想获取标准的GMT 格林威治时间就要用gmdate()函数了</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Converts a local Unix timestamp to GMT</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param   int Unix timestamp</span></span><br><span class="line"><span class="comment">     * @return  int</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">local_to_gmt</span>(<span class="params">$time = <span class="string">''</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> ($time === <span class="string">''</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            $time = time();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> mktime(</span><br><span class="line">            gmdate(<span class="string">'G'</span>, $time),</span><br><span class="line">            gmdate(<span class="string">'i'</span>, $time),</span><br><span class="line">            gmdate(<span class="string">'s'</span>, $time),</span><br><span class="line">            gmdate(<span class="string">'n'</span>, $time),</span><br><span class="line">            gmdate(<span class="string">'j'</span>, $time),</span><br><span class="line">            gmdate(<span class="string">'Y'</span>, $time)</span><br><span class="line">        );</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">   date_default_timezone_set(<span class="string">'Asia/Calcutta'</span>);</span><br><span class="line">   </span><br><span class="line">   $current_date = date(<span class="string">"Y/m/d g:i A"</span>);</span><br><span class="line">   </span><br><span class="line">   $ist_date = DateTime::createFromFormat(</span><br><span class="line">                           <span class="string">'"Y/m/d g:i A"'</span>,</span><br><span class="line">                           $current_date,</span><br><span class="line">                           <span class="keyword">new</span> DateTimeZone(<span class="string">'Asia/Calcutta'</span>)</span><br><span class="line">                       );</span><br><span class="line">   </span><br><span class="line">   $utc_date = clone $ist_date;</span><br><span class="line">   $utc_date-&gt;setTimeZone(<span class="keyword">new</span> DateTimeZone(<span class="string">'UTC'</span>));</span><br><span class="line">   </span><br><span class="line">   echo <span class="string">'UTC:  '</span> . $utc_date-&gt;format(<span class="string">'Y-m-d g:i A'</span>); </span><br><span class="line">    $time = gmmktime();</span><br><span class="line">    echo date(<span class="string">"Y-m-d H:i:s"</span>, $time); </span><br><span class="line">    date_default_timezone_set(<span class="string">"UTC"</span>);</span><br><span class="line">date(<span class="string">"Y-m-d H:i:s"</span>, time() - date(<span class="string">"Z"</span>))</span><br><span class="line">date_default_timezone_set(<span class="string">'Australia/Brisbane'</span>);</span><br><span class="line">echo gmdate(<span class="string">'c'</span>);<span class="comment">///2018-01-12T16:10:11+00:00</span></span><br><span class="line">最好是一直使用 UTC 时间。服务器使用，自己开发默认也是，然后存入数据库也是，这样的话把数据显示给用户看的话转换为适当时区的日期和时间就行了。</span><br><span class="line">时间戳是指格林尼治时间（GMT）<span class="number">1970</span>年<span class="number">01</span>月<span class="number">01</span>日<span class="number">00</span>时<span class="number">00</span>分<span class="number">00</span>秒到当前时间的总秒数。https:<span class="comment">//www.php.net/manual/en/timezones.php</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">st_date</span>(<span class="params">$format,$timestamp = false</span>) </span>&#123;</span><br><span class="line">  $timestamp = is_numberic($timestamp) ? $timestamp : time();</span><br><span class="line">  <span class="keyword">return</span> gmdate($format,$timestamp + <span class="number">8</span>*<span class="number">3600</span>);</span><br><span class="line">&#125;</span><br><span class="line">这样就可以保证这段代码无论在哪里，都可以输出东八区的时间。</span><br><span class="line">$triggerOn = <span class="string">'04/01/2013 03:08 PM'</span>;</span><br><span class="line">$user_tz = <span class="string">'America/Los_Angeles'</span>;</span><br><span class="line"></span><br><span class="line">echo $triggerOn; <span class="comment">// echoes 04/01/2013 03:08 PM</span></span><br><span class="line"></span><br><span class="line">$schedule_date = <span class="keyword">new</span> DateTime($triggerOn, <span class="keyword">new</span> DateTimeZone($user_tz) );</span><br><span class="line">$schedule_date-&gt;setTimeZone(<span class="keyword">new</span> DateTimeZone(<span class="string">'UTC'</span>));</span><br><span class="line">$triggerOn =  $schedule_date-&gt;format(<span class="string">'Y-m-d H:i:s'</span>);</span><br><span class="line"></span><br><span class="line">echo $triggerOn; <span class="comment">// echoes 2013-04-01 22:08:00</span></span><br><span class="line"></span><br><span class="line">格林威治时间（Greenwich Mean Time, GMT）。</span><br><span class="line"></span><br><span class="line">通用协调时间（Universal Time Coordinated, UTC）。UTC的表示方式为：年（y）、月（m）、日（d）、时（h）、分（min）、秒（s），均用数字表示。若以「世界标准时间」的角度来说，UTC比GMT来得更加精准： *UTC = GMT +<span class="regexp">/- 0.9* 。</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">本地时间(locale time)：很显然，本地时间跟时区(timezone)有关： *本地时间 = UTC + 时区* 。例如，中国北京标准时间（忽略GMT和UTC的差异）： *CST = GMT + 8 = UTC + 8* 。</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">Unix时间戳(Unix timestamp)，又称 Unix时间(Unix time) 或 POSIX时间(POSIX time) ，它从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数。</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">$usersNow = new DateTime('now', new DateTimeZone("+8"));/</span><span class="regexp">/php5.5+</span></span><br><span class="line"><span class="regexp">echo $usersNow-&gt;format(DateTime::RFC3339);/</span><span class="regexp">/2019-04-18T17:16:12+08:00</span></span><br><span class="line"><span class="regexp">$usersNow = new DateTime('now', new DateTimeZone('+0300'));</span></span><br><span class="line"><span class="regexp">$original = new DateTime("now", new DateTimeZone('UTC'));</span></span><br><span class="line"><span class="regexp">$timezoneName = timezone_name_from_abbr("", 3*3600, false);</span></span><br><span class="line"><span class="regexp">$modified = $original-&gt;setTimezone(new DateTimezone($timezoneName));</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">$date = date_create('2000-01-01', timezone_open('Pacific/</span>Nauru<span class="string">'));</span></span><br><span class="line"><span class="string">echo date_format($date, '</span>Y-m-d H:i:sP<span class="string">') . "\n";</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">date_timezone_set($date, timezone_open('</span>Pacific/Chatham<span class="string">'));</span></span><br><span class="line"><span class="string">echo date_format($date, '</span>Y-m-d H:i:sP<span class="string">') . "\n";</span></span><br><span class="line"><span class="string">为什么要时间戳？因为从0开始运行的秒数永远相等，即使出现润秒，也并不影响时间戳。</span></span><br><span class="line"><span class="string">php在使用date函数的时候，会依照所在时区去进行计算。date()进行输出的时间，就是我们所说的本地时间。</span></span><br><span class="line"><span class="string">可变：date()不可变：time()、gmdate()</span></span><br><span class="line"><span class="string">// 方法1date('</span>Y-m-d H:i:s<span class="string">',strtotime(gmdate('</span>Y-m-d H:i:s<span class="string">').'</span> +<span class="number">8</span> hours<span class="string">'));</span></span><br><span class="line"><span class="string">// 方法2（推荐）gmdate('</span>Y-m-d H:i:s<span class="string">',time() + 8*3600);</span></span><br><span class="line"><span class="string">$gmt_date = date('</span>Y-m-d H:i:s<span class="string">',time() - date('</span>Z<span class="string">'));</span></span><br><span class="line"><span class="string">$local_time = gmdate('</span>Y-m-d H:i:s<span class="string">',time() + date('</span>Z<span class="string">'));</span></span><br><span class="line"><span class="string">但这和$local_time = date('</span>Y-m-d H:i:s<span class="string">');没有任何结果上的区别。</span></span><br><span class="line"><span class="string">只保存timestamp，也就是time()，它的值是固定的，不随着时区的调整而改变，即使更换了服务器，它的误差也很小，所以有利于今后将程序分发部署到不同的服务器上面。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">          function toTimeZone($src, $from_tz = '</span>America/Denver<span class="string">', $to_tz = '</span>Asia/Shanghai<span class="string">', $fm = '</span>Y-m-d H:i:s<span class="string">') &#123;</span></span><br><span class="line"><span class="string">              $datetime = new DateTime($src, new DateTimeZone($from_tz));</span></span><br><span class="line"><span class="string">              $datetime-&gt;setTimezone(new DateTimeZone($to_tz));</span></span><br><span class="line"><span class="string">              return $datetime-&gt;format($fm);</span></span><br><span class="line"><span class="string">          &#125;                                                     </span></span><br><span class="line"><span class="string">$time_zone = "GMT+8";</span></span><br><span class="line"><span class="string">$time = time();</span></span><br><span class="line"><span class="string">$date = date_create(date("Y-m-d H:i", $time), timezone_open('</span>UTC<span class="string">'));</span></span><br><span class="line"><span class="string">$date = date_timezone_set($date, timezone_open($time_zone));</span></span><br><span class="line"><span class="string">$date = date_format($date, '</span>Y-m-d H:i<span class="string">');</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">echo date("Y-m-d H:i:s"); </span></span><br><span class="line"><span class="string">// date() 返回的是:　当前(这一刻 time()函数执行/返回时) GMT标准时间 的"本地化时间" 的自定义格式时间</span></span><br><span class="line"><span class="string">// date()跟php系统设置的时区有关!</span></span><br><span class="line"><span class="string">echo gmdate("Y-m-d H:i:s");</span></span><br><span class="line"><span class="string">// gmdate() 返回的是:　当前(这一刻 time()函数执行/返回时) GMT标准时间  的自定义格式时间</span></span><br><span class="line"><span class="string">// gmdate() 跟你现在所处的位置无关, 跟php系统设置的时区无关!</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">也就说，　date()和gmdate()的区别, 仅仅在于 处理的时间 是不同的!</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">INSERT INTO `table` (id, time) VALUES(NULL, UNIX_TIMESTAMP()); </span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">// or http://cn.voidcc.com/question/p-fvgnwppc-bc.html</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$time = time(); </span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">$query = "INSERT INTO `table` (id, time) VALUES(NULL, $time); </span></span><br><span class="line"><span class="string">如果你想从数据库中选择它作为一个DATETIME，你可以这样做：</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">SELECT *, FROM_UNIXTIME(time) as dt FROM `table` WHERE 1 </span></span><br><span class="line"><span class="string">产生的dt栏会格式为yyyy-MM-dd hh:mm:ss。</span></span><br></pre></td></tr></table></figure>
<h3 id="You-have-new-mail-in-var-spool-mail-root"><a href="#You-have-new-mail-in-var-spool-mail-root" class="headerlink" title="You have new mail in /var/spool/mail/root"></a>You have new mail in /var/spool/mail/root</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">tac /<span class="keyword">var</span>/spool/mail/root |more</span><br><span class="line">PHP Fatal error:  require_once(): Failed opening required <span class="string">'../Redis/RedisManager.php'</span> (include_path=<span class="string">'.:/usr/local/lib/php</span></span><br><span class="line"><span class="string">/:/usr/share/pear:/usr/share/php'</span>) <span class="keyword">in</span> /cron/test.php on line <span class="number">8</span></span><br><span class="line"></span><br><span class="line">crotnab -e</span><br><span class="line">*<span class="regexp">/5 * * * 1-6 cd /</span>cron/;<span class="regexp">/usr/</span>local/bin/php /cron/test.php -t <span class="number">15</span> </span><br><span class="line"></span><br><span class="line">tail -f /<span class="keyword">var</span>/log/cron</span><br><span class="line">Apr <span class="number">18</span> <span class="number">18</span>:<span class="number">00</span>:<span class="number">02</span> xk<span class="number">-6</span><span class="number">-240</span>-a8 CROND[<span class="number">23941</span>]: (root) CMD (<span class="regexp">/usr/</span>lib64/sa/sa1 <span class="number">1</span> <span class="number">1</span>)</span><br><span class="line"> Apr <span class="number">18</span> <span class="number">18</span>:<span class="number">00</span>:<span class="number">02</span> xk<span class="number">-6</span><span class="number">-240</span>-a8 CROND[<span class="number">23942</span>]: (root) CMD (cd /cron/;<span class="regexp">/usr/</span>local/bin/php /cron/test.php -t <span class="number">15</span></span><br></pre></td></tr></table></figure>
<h3 id="对二维数组进行排序"><a href="#对二维数组进行排序" class="headerlink" title="对二维数组进行排序"></a>对二维数组进行排序</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 对二维数组进行按字段排序</span></span><br><span class="line"><span class="comment"> * @param array $array 要排序的二维数组</span></span><br><span class="line"><span class="comment"> * @param bool $orderby 根据该字段（二维数组单个元素中的键名）排序</span></span><br><span class="line"><span class="comment"> * @param string $order 排序方式，asc:升序；desc:降序（默认）</span></span><br><span class="line"><span class="comment"> * @param string $children 子元素字段（键名），当元素含有该字段时，进行递归排序</span></span><br><span class="line"><span class="comment"> * @return array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">array_orderby</span>(<span class="params">&amp;$array,$orderby = null,$order = <span class="string">'desc'</span>,$children = false</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span>($orderby == <span class="literal">null</span>)</span><br><span class="line">    <span class="keyword">return</span> $array;</span><br><span class="line">  $key_value = $new_array = array();</span><br><span class="line"></span><br><span class="line">  foreach($array <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">    $key_value[$k] = $v[$orderby];</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span>($order == <span class="string">'asc'</span>) &#123;</span><br><span class="line">    asort($key_value);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span> &#123;</span><br><span class="line">    arsort($key_value);</span><br><span class="line">  &#125;</span><br><span class="line">  reset($key_value);</span><br><span class="line"></span><br><span class="line">  foreach($key_value <span class="keyword">as</span> $k =&gt; $v) &#123;</span><br><span class="line">    $new_array[$k] = $array[$k];</span><br><span class="line">    <span class="comment">// 如果有children</span></span><br><span class="line">    <span class="keyword">if</span>($children &amp;&amp; isset($new_array[$k][$children])) &#123;</span><br><span class="line">      $new_array[$k][$children] = array_sort($new_array[$k][$children]);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  $new_array = array_values($new_array); <span class="comment">// 使键名从0开始升序排列</span></span><br><span class="line">  $array = $new_array;</span><br><span class="line">  <span class="keyword">return</span> $new_array;</span><br><span class="line">&#125;</span><br><span class="line">$a = array(</span><br><span class="line">  array(<span class="string">'name'</span> =&gt; <span class="string">'Kenvi'</span>,<span class="string">'age'</span> =&gt; <span class="number">18</span>),</span><br><span class="line">  array(<span class="string">'name'</span> =&gt; <span class="string">'Nance'</span>,<span class="string">'age'</span> =&gt; <span class="number">21</span>),</span><br><span class="line">  .....</span><br><span class="line">);</span><br><span class="line">array_orderby($a,<span class="string">'age'</span>,<span class="string">'asc'</span>); <span class="comment">// 或者$a = array_orderby($a,'age','asc');</span></span><br><span class="line">$b = array(</span><br><span class="line">  array(</span><br><span class="line">    <span class="string">'id'</span> =&gt; <span class="number">12</span>,<span class="string">'count'</span> =&gt; <span class="number">36</span>,</span><br><span class="line">    <span class="string">'children'</span> =&gt; array(</span><br><span class="line">      array(<span class="string">'id'</span> =&gt; <span class="number">76</span>,<span class="string">'count'</span> =&gt; <span class="number">40</span>),</span><br><span class="line">      array(<span class="string">'id'</span> =&gt; <span class="number">98</span>,<span class="string">'count'</span> =&gt; <span class="number">100</span>)</span><br><span class="line">    )</span><br><span class="line">  ),</span><br><span class="line">  array(<span class="string">'id'</span> =&gt; <span class="number">19</span>,<span class="string">'count'</span> =&gt; <span class="number">87</span>),</span><br><span class="line">  ......</span><br><span class="line">);</span><br><span class="line">array_orderby($b,<span class="string">'count'</span>,<span class="string">'desc'</span>,<span class="string">'children'</span>);</span><br><span class="line">https:<span class="comment">//www.tangshuang.net/2077.html</span></span><br></pre></td></tr></table></figure>
<h3 id="mysql-orderby-limit-翻页数据重复"><a href="#mysql-orderby-limit-翻页数据重复" class="headerlink" title="mysql orderby limit 翻页数据重复"></a>mysql orderby limit 翻页数据重复</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">SELECT <span class="string">`post_title`</span>,<span class="string">`post_date`</span> FROM post WHERE <span class="string">`post_status`</span>=<span class="string">'publish'</span> ORDER BY view_count desc LIMIT <span class="number">5</span>,<span class="number">5</span></span><br><span class="line">使用上述SQL查询的时候，很有可能出现和LIMIT <span class="number">0</span>,<span class="number">5</span>相同的某条记录，而如果使用：</span><br><span class="line"></span><br><span class="line">SELECT * FROM post WHERE <span class="string">`post_status`</span>=<span class="string">'publish'</span> ORDER BY view_count desc LIMIT <span class="number">5</span>,<span class="number">5</span></span><br><span class="line">则不会出现重复的情况。但是，由于post表的字段很多，我仅仅希望用这两个字段，不想把post_content也查出来。为了解决这个情况，我在ORDER BY后面使用了两个排序条件来解决这个问题。</span><br><span class="line"></span><br><span class="line">SELECT <span class="string">`post_title`</span>,<span class="string">`post_date`</span> FROM post WHERE <span class="string">`post_status`</span>=<span class="string">'publish'</span> ORDER BY view_count desc,ID asc LIMIT <span class="number">5</span>,<span class="number">5</span></span><br><span class="line">按理来说，mysql的排序默认情况下是以主键ID作为排序条件的，也就是说，如果在view_count相等的情况下，主键ID作为默认的排序条件，不需要我们多此一举加ID asc。但是事实就是，mysql再order by和limit混用的时候，出现了排序的混乱情况。其后的机理我尚不得而知，在阅读这篇文章后，好像有所领悟，下面做一下猜测。</span><br><span class="line"></span><br><span class="line">这篇文章的解释是：https:<span class="comment">//www.tangshuang.net/1827.html</span></span><br><span class="line"></span><br><span class="line">在MySQL <span class="number">5.6</span>的版本上，优化器在遇到order by limit语句的时候，做了一个优化，即使用了priority queue。……</span><br><span class="line"></span><br><span class="line">使用 priority queue 的目的，就是在不能使用索引有序性的时候，如果要排序，并且使用了limit n，那么只需要在排序的过程中，保留n条记录即可，这样虽然不能解决所有记录都需要排序的开销，但是只需要 sort buffer 少量的内存就可以完成排序。</span><br><span class="line"></span><br><span class="line">之所以<span class="number">5.6</span>出现了第二页数据重复的问题，是因为 priority queue 使用了堆排序的排序方法，而堆排序是一个不稳定的排序方法，也就是相同的值可能排序出来的结果和读出来的数据顺序不一致。</span><br><span class="line"></span><br><span class="line"><span class="number">5.5</span> 没有这个优化，所以也就不会出现这个问题。</span><br><span class="line">也就是说，mysql5<span class="number">.5</span>是不存在本文提到的问题的，<span class="number">5.6</span>版本之后才出现了这种情况。</span><br><span class="line"></span><br><span class="line">我们再看下mysql解释sql语言时的执行顺序：</span><br><span class="line"></span><br><span class="line">(<span class="number">7</span>) SELECT </span><br><span class="line">(<span class="number">8</span>) DISTINCT &lt;select_list&gt;</span><br><span class="line">(<span class="number">1</span>) FROM &lt;left_table&gt;</span><br><span class="line">(<span class="number">3</span>) &lt;join_type&gt; JOIN &lt;right_table&gt;</span><br><span class="line">(<span class="number">2</span>) ON &lt;join_condition&gt;</span><br><span class="line">(<span class="number">4</span>) WHERE &lt;where_condition&gt;</span><br><span class="line">(<span class="number">5</span>) GROUP BY &lt;group_by_list&gt;</span><br><span class="line">(<span class="number">6</span>) HAVING &lt;having_condition&gt;</span><br><span class="line">(<span class="number">9</span>) ORDER BY &lt;order_by_condition&gt;</span><br><span class="line">(<span class="number">10</span>) LIMIT &lt;limit_number&gt;</span><br><span class="line">在我们本文的案例sql中，执行顺序依次为form... where... select... order by... limit...</span><br><span class="line"></span><br><span class="line">由于上述priority queue的原因，在完成select之后，所有记录是以堆排序的方法排列的，在进行order by时，仅把view_count值大的往前移动。但由于limit的因素，排序过程中只需要保留到<span class="number">5</span>条记录即可，view_count并不具备索引有序性，所以当第二页数据要展示时，mysql见到哪一条就拿哪一条，因此，当排序值相同的时候，第一次排序是随意排的，第二次再执行该sql的时候，其结果应该和第一次结果一样。</span><br></pre></td></tr></table></figure>
<h3 id="消息队列处理高并发"><a href="#消息队列处理高并发" class="headerlink" title="消息队列处理高并发"></a>消息队列处理高并发</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">当用户点击按钮提交购买请求时，并不直接马上执行购买请求动作，而是将请求动作存入消息列队，用户的请求到此结束，而在服务器后台，从消息列队中逐一取出请求记录，再进行数据库操作，完成处理之后，将处理结果返回给用户。由于消息队列的存取是先进先出（和栈相反），因此，所有处理将按请求顺序进行处理。由于处理是在后台一个一个完成，因此不会对服务器和数据库造成巨大压力。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">$redis = <span class="keyword">new</span> Redis();</span><br><span class="line">$redis-&gt;connect(<span class="string">'127.0.0.1'</span>,<span class="number">6379</span>);</span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  $redis-&gt;LPUSH(<span class="string">'click'</span>,$user_id);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">catch</span>(Exception $e) &#123;</span><br><span class="line">  echo $e-&gt;getMessage();</span><br><span class="line">&#125;</span><br><span class="line">$redis = <span class="keyword">new</span> Redis();</span><br><span class="line">$redis-&gt;pconnect(<span class="string">'127.0.0.1'</span>,<span class="number">6379</span>);</span><br><span class="line"><span class="keyword">while</span>(<span class="number">1</span>) &#123;</span><br><span class="line">  <span class="keyword">try</span>&#123;</span><br><span class="line">    $value = $redis-&gt;LPOP(<span class="string">'click'</span>);</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">      利用$value进行逻辑和数据处理</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">catch</span>(Exception $e) &#123;</span><br><span class="line">    echo $e-&gt;getMessage();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="获取这两个字符串前面的相同部分"><a href="#获取这两个字符串前面的相同部分" class="headerlink" title="获取这两个字符串前面的相同部分"></a>获取这两个字符串前面的相同部分</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">异或运算是一种非常特殊的运算，简单的说就是“如果两个值相同，返回<span class="number">0</span>|<span class="literal">false</span>，如果两个值不同，则返回<span class="number">1</span>|<span class="literal">true</span>”。</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">samestrin</span>(<span class="params">$str1, $str2</span>) </span>&#123;</span><br><span class="line">  $pos = strspn($str1 ^ $str2, <span class="string">"\0"</span>);</span><br><span class="line">  <span class="keyword">if</span>($pos) &#123;</span><br><span class="line">    <span class="keyword">return</span> substr($str1, <span class="number">0</span>, $pos);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">&#125;https:<span class="comment">//www.tangshuang.net/82.html</span></span><br><span class="line">上述代码中，关键部分已经用红色表示出来了。一般情况下，当你对两个字符串进行异或操作的时候，相同的字符的异或结果是<span class="literal">null</span>(“\<span class="number">0</span>”)，所以我们只要找出第一个非<span class="literal">null</span>(“\<span class="number">0</span>”)字符就可以了。如此优雅的操作，得益于计算机领域的异或运算，如若没有这种算法，我们可能需要写一大堆代码来进行匹配对比。</span><br><span class="line"> samestrin(<span class="string">'ab'</span>,<span class="string">'abd'</span>)</span><br><span class="line">=&gt; <span class="string">"ab"</span></span><br></pre></td></tr></table></figure>
<h3 id="树形结构算法"><a href="#树形结构算法" class="headerlink" title="树形结构算法"></a>树形结构算法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 构建层级（树状）数组</span></span><br><span class="line"><span class="comment"> * @param array $array 要进行处理的一维数组，经过该函数处理后，该数组自动转为树状数组</span></span><br><span class="line"><span class="comment"> * @param string $pid 父级ID的字段名</span></span><br><span class="line"><span class="comment"> * @return array|bool</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">array_tree</span>(<span class="params">&amp;$array, $pid = <span class="string">'pid'</span></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 子元素计数器</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">array_children_count</span>(<span class="params">$array,$pid</span>) </span>&#123;</span><br><span class="line">    $counter = array();</span><br><span class="line">    foreach($array <span class="keyword">as</span> $item) &#123;</span><br><span class="line">      $count = isset($counter[$item[$pid]]) ? $counter[$item[$pid]] : <span class="number">0</span>;</span><br><span class="line">      $count ++;</span><br><span class="line">      $counter[$item[$pid]] = $count;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $counter;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 把元素插入到对应的父元素children字段</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">array_child_append</span>(<span class="params">$parent,$pid,$child</span>) </span>&#123;</span><br><span class="line">    foreach($parent <span class="keyword">as</span> &amp;$item) &#123;</span><br><span class="line">      <span class="keyword">if</span>($item[<span class="string">'id'</span>] == $pid) &#123;</span><br><span class="line">        <span class="keyword">if</span>(!isset($item[<span class="string">'children'</span>])) $item[<span class="string">'children'</span>] = array();</span><br><span class="line">        $item[<span class="string">'children'</span>][] = $child;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $parent;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// 开始程序</span></span><br><span class="line">  $counter = array_children_count($array,$pid);</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 如果顶级元素为0个，那么直接返回false</span></span><br><span class="line">  <span class="keyword">if</span>($counter[<span class="number">0</span>] == <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 准备顶级元素</span></span><br><span class="line">  $tree = array();</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 位移</span></span><br><span class="line">  <span class="keyword">while</span>(isset($counter[<span class="number">0</span>]) &amp;&amp; $counter[<span class="number">0</span>] &gt; <span class="number">0</span>) &#123; <span class="comment">// 如果顶级栏目的子元素计数器仍然大于0，那么仍然往下执行循环</span></span><br><span class="line">    $temp = array_shift($array);</span><br><span class="line">    <span class="keyword">if</span>(isset($counter[$temp[<span class="string">'id'</span>]]) &amp;&amp; $counter[$temp[<span class="string">'id'</span>]] &gt; <span class="number">0</span>) &#123; <span class="comment">// 如果数组的第一个元素的子元素个数大于0，那么把该元素放置到数组的末端</span></span><br><span class="line">      array_push($array,$temp);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span> &#123; <span class="comment">// 相反，如果该数组的第一个元素没有子元素，那么把该元素移动到其父元素的children字段中，同时，该元素从原数组中被删除</span></span><br><span class="line">      <span class="keyword">if</span>($temp[$pid] == <span class="number">0</span>)</span><br><span class="line">        $tree[] = $temp;</span><br><span class="line">      <span class="keyword">else</span></span><br><span class="line">        $array = array_child_append($array,$temp[$pid],$temp);</span><br><span class="line">    &#125;</span><br><span class="line">    $counter = array_children_count($array,$pid);</span><br><span class="line">  &#125;</span><br><span class="line">  $array = $tree;</span><br><span class="line">  <span class="keyword">return</span> $tree;</span><br><span class="line">&#125;</span><br><span class="line">$arr = array(</span><br><span class="line">  array(<span class="string">'id'</span> =&gt; <span class="number">53</span>,<span class="string">'pid'</span> =&gt; <span class="number">0</span>),</span><br><span class="line">  array(<span class="string">'id'</span> =&gt; <span class="number">64</span>,<span class="string">'pid'</span> =&gt; <span class="number">53</span>),</span><br><span class="line">  array(<span class="string">'id'</span> =&gt; <span class="number">70</span>,<span class="string">'pid'</span> =&gt; <span class="number">42</span>)</span><br><span class="line">);</span><br><span class="line"><span class="comment">//$r=array_tree($arr,'pid');print_r($r);</span></span><br><span class="line"></span><br><span class="line">$demo1 = array(</span><br><span class="line">  <span class="number">1</span> =&gt; array(<span class="string">'id'</span> =&gt; <span class="number">1</span>,<span class="string">'title'</span> =&gt; <span class="string">'title1'</span>,<span class="string">'pid'</span> =&gt; <span class="number">0</span>),</span><br><span class="line">  <span class="number">2</span> =&gt; array(<span class="string">'id'</span> =&gt; <span class="number">2</span>,<span class="string">'title'</span> =&gt; <span class="string">'title2'</span>,<span class="string">'pid'</span> =&gt; <span class="number">1</span>),</span><br><span class="line">  <span class="number">3</span> =&gt; array(<span class="string">'id'</span> =&gt; <span class="number">3</span>,<span class="string">'title'</span> =&gt; <span class="string">'title3'</span>,<span class="string">'pid'</span> =&gt; <span class="number">1</span>),</span><br><span class="line">  <span class="number">4</span> =&gt; array(<span class="string">'id'</span> =&gt; <span class="number">4</span>,<span class="string">'title'</span> =&gt; <span class="string">'title4'</span>,<span class="string">'pid'</span> =&gt; <span class="number">2</span>),</span><br><span class="line">  <span class="number">5</span> =&gt; array(<span class="string">'id'</span> =&gt; <span class="number">5</span>,<span class="string">'title'</span> =&gt; <span class="string">'title5'</span>,<span class="string">'pid'</span> =&gt; <span class="number">2</span>),</span><br><span class="line">  <span class="number">6</span> =&gt; array(<span class="string">'id'</span> =&gt; <span class="number">6</span>,<span class="string">'title'</span> =&gt; <span class="string">'title6'</span>,<span class="string">'pid'</span> =&gt; <span class="number">0</span>)</span><br><span class="line">);</span><br><span class="line">$demo1 = array_tree($demo1);<span class="comment">// 或者直接使用array_tree($demo1);，无需返回值https://www.tangshuang.net/2073.html https://3v4l.org/kngT8</span></span><br><span class="line">print_r($demo1);</span><br><span class="line"><span class="built_in">Array</span></span><br><span class="line">(</span><br><span class="line">    [<span class="number">0</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">        (</span><br><span class="line">            [name] =&gt; Nance</span><br><span class="line">            [age] =&gt; <span class="number">21</span></span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [<span class="number">1</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">        (</span><br><span class="line">            [name] =&gt; Kenvi</span><br><span class="line">            [age] =&gt; <span class="number">18</span></span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">)</span><br><span class="line"><span class="built_in">Array</span></span><br><span class="line">(</span><br><span class="line">    [<span class="number">0</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">        (</span><br><span class="line">            [id] =&gt; <span class="number">6</span></span><br><span class="line">            [title] =&gt; title6</span><br><span class="line">            [pid] =&gt; <span class="number">0</span></span><br><span class="line">        )</span><br><span class="line"></span><br><span class="line">    [<span class="number">1</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">        (</span><br><span class="line">            [id] =&gt; <span class="number">1</span></span><br><span class="line">            [title] =&gt; title1</span><br><span class="line">            [pid] =&gt; <span class="number">0</span></span><br><span class="line">            [children] =&gt; <span class="built_in">Array</span></span><br><span class="line">                (</span><br><span class="line">                    [<span class="number">0</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">                        (</span><br><span class="line">                            [id] =&gt; <span class="number">3</span></span><br><span class="line">                            [title] =&gt; title3</span><br><span class="line">                            [pid] =&gt; <span class="number">1</span></span><br><span class="line">                        )</span><br><span class="line"></span><br><span class="line">                    [<span class="number">1</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">                        (</span><br><span class="line">                            [id] =&gt; <span class="number">2</span></span><br><span class="line">                            [title] =&gt; title2</span><br><span class="line">                            [pid] =&gt; <span class="number">1</span></span><br><span class="line">                            [children] =&gt; <span class="built_in">Array</span></span><br><span class="line">                                (</span><br><span class="line">                                    [<span class="number">0</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">                                        (</span><br><span class="line">                                            [id] =&gt; <span class="number">4</span></span><br><span class="line">                                            [title] =&gt; title4</span><br><span class="line">                                            [pid] =&gt; <span class="number">2</span></span><br><span class="line">                                        )</span><br><span class="line"></span><br><span class="line">                                    [<span class="number">1</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">                                        (</span><br><span class="line">                                            [id] =&gt; <span class="number">5</span></span><br><span class="line">                                            [title] =&gt; title5</span><br><span class="line">                                            [pid] =&gt; <span class="number">2</span></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">array_tree</span>(<span class="params">$array</span>) </span>&#123;</span><br><span class="line">    $result = array();</span><br><span class="line">    $tmp = array();</span><br><span class="line">    foreach($array <span class="keyword">as</span> $item) &#123;</span><br><span class="line">      <span class="keyword">if</span>($item[<span class="string">'parent_id'</span>] == <span class="number">0</span>) &#123;</span><br><span class="line">        $i = count($result);</span><br><span class="line">        $result[$i] = $item;</span><br><span class="line">        $id = $item[<span class="string">'id'</span>];</span><br><span class="line">        $tmp[$id] = &amp;$result[$i];<span class="comment">//这一句保证了当无论$tmp[$id]还是$result[$i]发生变化，都会让另外一个值同时发生变化。而 $tmp[$parent_id]['children'][$i] = $item; 实际上导致$result[$parent_id]发生了变化。</span></span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">else</span> &#123;</span><br><span class="line">        $id = $item[<span class="string">'id'</span>];</span><br><span class="line">        $parent_id = $item[<span class="string">'parent_id'</span>];</span><br><span class="line">        $parent = $tmp[$parent_id];</span><br><span class="line">        $i = count($parent[<span class="string">'children'</span>]);</span><br><span class="line">        $tmp[$parent_id][<span class="string">'children'</span>][$i] = $item;</span><br><span class="line">        $tmp[$id] = &amp;$tmp[$parent_id][<span class="string">'children'</span>][$i];</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> $result;</span><br><span class="line">  &#125;</span><br><span class="line">  当$b = &amp;$a时，$b和$a同时引用同一个内容（指针指向同一块内存），无论$a或$b谁发生变化，这个内容都会发生变化，进而呈现为$a和$b保持同步的变化。</span><br><span class="line">  https:<span class="comment">//www.tangshuang.net/1701.html</span></span><br><span class="line">  print_r(array_tree($demo1));</span><br><span class="line">    <span class="built_in">Array</span></span><br><span class="line">    (</span><br><span class="line">        [<span class="number">0</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">            (</span><br><span class="line">                [id] =&gt; <span class="number">1</span></span><br><span class="line">                [title] =&gt; title1</span><br><span class="line">                [parent_id] =&gt; <span class="number">0</span></span><br><span class="line">                [children] =&gt; <span class="built_in">Array</span></span><br><span class="line">                    (</span><br><span class="line">                        [<span class="number">0</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">                            (</span><br><span class="line">                                [id] =&gt; <span class="number">2</span></span><br><span class="line">                                [title] =&gt; title2</span><br><span class="line">                                [parent_id] =&gt; <span class="number">1</span></span><br><span class="line">                                [children] =&gt; <span class="built_in">Array</span></span><br><span class="line">                                    (</span><br><span class="line">                                        [<span class="number">0</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">                                            (</span><br><span class="line">                                                [id] =&gt; <span class="number">4</span></span><br><span class="line">                                                [title] =&gt; title4</span><br><span class="line">                                                [parent_id] =&gt; <span class="number">2</span></span><br><span class="line">                                            )</span><br><span class="line">    </span><br><span class="line">                                        [<span class="number">1</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">                                            (</span><br><span class="line">                                                [id] =&gt; <span class="number">5</span></span><br><span class="line">                                                [title] =&gt; title5</span><br><span class="line">                                                [parent_id] =&gt; <span class="number">2</span></span><br><span class="line">                                            )</span><br><span class="line">    </span><br><span class="line">                                    )</span><br><span class="line">    </span><br><span class="line">                            )</span><br><span class="line">    </span><br><span class="line">                        [<span class="number">1</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">                            (</span><br><span class="line">                                [id] =&gt; <span class="number">3</span></span><br><span class="line">                                [title] =&gt; title3</span><br><span class="line">                                [parent_id] =&gt; <span class="number">1</span></span><br><span class="line">                            )</span><br><span class="line">    </span><br><span class="line">                    )</span><br><span class="line">    </span><br><span class="line">            )</span><br><span class="line">    </span><br><span class="line">        [<span class="number">1</span>] =&gt; <span class="built_in">Array</span></span><br><span class="line">            (</span><br><span class="line">                [id] =&gt; <span class="number">6</span></span><br><span class="line">                [title] =&gt; title6</span><br><span class="line">                [parent_id] =&gt; <span class="number">0</span></span><br><span class="line">            )</span><br><span class="line">    </span><br><span class="line">    )</span><br><span class="line">    $tmp[$id] = &amp;$tmp[$parent_id][<span class="string">'children'</span>][$i];这一句起到了关键性作用。加上这一句之后，我们再来跑一遍array(<span class="string">'id'</span> =&gt; <span class="number">23</span>,<span class="string">'parent_id'</span> =&gt; <span class="number">12</span>,...)这个元素。</span><br><span class="line">    </span><br><span class="line">    [<span class="number">2</span>]=&gt; array(<span class="string">'id'</span> =&gt; <span class="number">12</span>,<span class="string">'parent_id'</span> =&gt; <span class="number">5</span>,...)</span><br><span class="line">    </span><br><span class="line">    $tmp[<span class="number">5</span>][<span class="string">'children'</span>][<span class="number">0</span>] = array(<span class="string">'id'</span> =&gt; <span class="number">12</span>,<span class="string">'parent_id'</span> =&gt; <span class="number">5</span>,...)  <span class="comment">// 重新从①开始演示</span></span><br><span class="line">    </span><br><span class="line">    $result[<span class="number">0</span>][<span class="string">'children'</span>][<span class="number">0</span>] = array(<span class="string">'id'</span> =&gt; <span class="number">12</span>,<span class="string">'parent_id'</span> =&gt; <span class="number">5</span>,...) <span class="comment">// 内部结果</span></span><br><span class="line">    </span><br><span class="line">    $tmp[<span class="number">12</span>] = &amp;$tmp[<span class="number">5</span>][<span class="string">'children'</span>][<span class="number">0</span>]  <span class="comment">// 第二个&amp;出现了</span></span><br><span class="line">    </span><br><span class="line">    [<span class="number">5</span>]=&gt; array(<span class="string">'id'</span> =&gt; <span class="number">23</span>,<span class="string">'parent_id'</span> =&gt; <span class="number">12</span>,...)</span><br><span class="line">    </span><br><span class="line">    $tmp[<span class="number">12</span>][<span class="string">'children'</span>][<span class="number">0</span>] = array(<span class="string">'id'</span> =&gt; <span class="number">23</span>,<span class="string">'parent_id'</span> =&gt; <span class="number">12</span>,...)</span><br><span class="line">    </span><br><span class="line">    由于第二个&amp;引用的原因，实际上发生了：</span><br><span class="line">    </span><br><span class="line">    $tmp[<span class="number">5</span>][<span class="string">'children'</span>][<span class="number">0</span>][<span class="string">'children'</span>][<span class="number">0</span>] = array(<span class="string">'id'</span> =&gt; <span class="number">23</span>,<span class="string">'parent_id'</span> =&gt; <span class="number">12</span>,...) <span class="comment">// 内部结果</span></span><br><span class="line">    </span><br><span class="line">    $result[<span class="number">0</span>][<span class="string">'children'</span>][<span class="number">0</span>][<span class="string">'children'</span>][<span class="number">0</span>] = array(<span class="string">'id'</span> =&gt; <span class="number">23</span>,<span class="string">'parent_id'</span> =&gt; <span class="number">12</span>,...) <span class="comment">// 内部结果</span></span><br><span class="line">    这个时候你可能已经看出了端倪。父子关系变成了 <span class="number">5</span> &gt; <span class="number">12</span> &gt; <span class="number">23</span>，而这组关系，全部存储在了id=<span class="number">5</span>的这个顶级元素中，以多重的children元素实现了父子孙结构。</span><br></pre></td></tr></table></figure>
<h3 id="PHP进程分支设计"><a href="#PHP进程分支设计" class="headerlink" title="PHP进程分支设计"></a>PHP进程分支设计</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/tangshuang/php-events-async-callback https://www.tangshuang.net/2086.html</span></span><br><span class="line">$pid = pcntl_fork();</span><br><span class="line"><span class="keyword">if</span>($pid &gt; <span class="number">0</span>)&#123; <span class="comment">//父进程代码</span></span><br><span class="line">  exit(<span class="number">0</span>);</span><br><span class="line">&#125;</span><br><span class="line">elseif($pid == <span class="number">0</span>) &#123; <span class="comment">//子进程代码</span></span><br><span class="line">  exit(<span class="number">0</span>);</span><br><span class="line">&#125;</span><br><span class="line">你会发现httpd或php-fpm的进程会增加，也就是说，pcntl其实是通过重新开启php进程，并通过其他机制实现进程之间的通信的。</span><br><span class="line">proc_open，popen也是利用httpd来实现多进程</span><br><span class="line"></span><br><span class="line">$proc=proc_open(<span class="string">"echo foo"</span>, array(  </span><br><span class="line">  array(<span class="string">"pipe"</span>,<span class="string">"r"</span>),  </span><br><span class="line">  array(<span class="string">"pipe"</span>,<span class="string">"w"</span>),  </span><br><span class="line">  array(<span class="string">"pipe"</span>,<span class="string">"w"</span>)</span><br><span class="line">), $pipes);  </span><br><span class="line">print stream_get_contents($pipes[<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line"><span class="built_in">require</span> <span class="string">'Events.php'</span>;</span><br><span class="line"><span class="built_in">require</span> <span class="string">'EventListener.class.php'</span>;</span><br><span class="line"></span><br><span class="line">$EventListener = <span class="keyword">new</span> EventListener(<span class="string">'timeout'</span>);</span><br><span class="line">$EventListener-&gt;add(<span class="string">'timeout'</span>,<span class="string">'setTimeout'</span>);</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">setTimeout</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  sleep(<span class="number">10</span>);</span><br><span class="line">  file_put_contents(<span class="string">'./log.txt'</span>,<span class="string">'延时回调成功，现在时间：'</span>.date(<span class="string">'Y-m-d H:i:s'</span>),FILE_APPEND);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 你自己的代码流程https://github.com/tangshuang/php-events-async-callback</span></span><br><span class="line"></span><br><span class="line">file_put_contents(<span class="string">'./log.txt'</span>,<span class="string">'延时操作开始，现在时间：'</span>.date(<span class="string">'Y-m-d H:i:s'</span>).<span class="string">"\r"</span>,LOCK_EX);</span><br><span class="line">$EventListener-&gt;run(<span class="string">'timeout'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="shell重启服务"><a href="#shell重启服务" class="headerlink" title="shell重启服务"></a>shell重启服务</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">在shell脚本中对服务进行判断，如果nginx或者httpd宕机了，就执行一条重启命令：</span><br><span class="line"></span><br><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line">#nginx</span><br><span class="line">ps -ef | grep nginx |grep -v grep &gt; <span class="regexp">/dev/</span><span class="literal">null</span></span><br><span class="line"><span class="keyword">if</span> [ $? != <span class="number">0</span> ];then</span><br><span class="line">       service nginx restart &gt; <span class="regexp">/dev/</span>nullfi</span><br><span class="line">#httpd</span><br><span class="line">ps -ef | grep httpd |grep -v grep &gt; <span class="regexp">/dev/</span><span class="literal">null</span></span><br><span class="line"><span class="keyword">if</span> [ $? != <span class="number">0</span> ];then</span><br><span class="line">       service httpd restart &gt; <span class="regexp">/dev/</span>nullfi</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-IoC-服务容器类管理工具"><a href="#Laravel-IoC-服务容器类管理工具" class="headerlink" title="Laravel IoC 服务容器类管理工具"></a>Laravel IoC 服务容器类管理工具</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br></pre></td><td class="code"><pre><span class="line">高层次的模块不应该依赖于低层次的模块，两者都应该依赖于抽象接口。</span><br><span class="line"></span><br><span class="line">抽象接口不应该依赖于具体实现。而具体实现则应该依赖于抽象接口。</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MySQLConnection</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">   <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 数据库连接</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">      var_dump(‘MYSQL Connection’);</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PasswordReminder</span></span></span><br><span class="line"><span class="class"></span>&#123;    </span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * @var MySQLConnection</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">     private $dbConnection;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">MySQLConnection $dbConnection</span>) </span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">      $<span class="keyword">this</span>-&gt;dbConnection = $dbConnection;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">大家常常会有一个误解，那就是依赖反转就只是依赖注入的另一种说法。但其实二者是不同的。在上面的代码示例中，尽管在 PasswordReminder 类中注入了 MySQLConnection 类，但它还是依赖于 MySQLConnection 类。</span><br><span class="line"></span><br><span class="line">然而，高层次模块 PasswordReminder 是不应该依赖于低层次模块 MySQLConnection 的。</span><br><span class="line"></span><br><span class="line">如果我们想要把 MySQLConnection 改成 MongoDBConnection，那我们就还得手动修改 PasswordReminder 类构造函数里的依赖。</span><br><span class="line"></span><br><span class="line">interface ConnectionInterface</span><br><span class="line">&#123;</span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">DbConnection</span> <span class="title">implements</span> <span class="title">ConnectionInterface</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 数据库连接</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"> public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"> </span>&#123;</span><br><span class="line">   var_dump(‘MYSQL Connection’);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">PasswordReminder</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * @var DBConnection</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    private $dbConnection;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">ConnectionInterface $dbConnection</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">      $<span class="keyword">this</span>-&gt;dbConnection = $dbConnection;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"> IoC 容器简单地理解为就是一个容器，里面装的是类的依赖。</span><br><span class="line">namespace App\Repositories;</span><br><span class="line"></span><br><span class="line">interface OrderRepositoryInterface </span><br><span class="line">&#123;</span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">getAll</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function">&#125;</span></span><br><span class="line"><span class="function"><span class="title">namespace</span> <span class="title">App</span>\<span class="title">Repositories</span>;</span></span><br><span class="line"><span class="function"></span></span><br><span class="line"><span class="function"><span class="title">class</span> <span class="title">DbOrderRepository</span> <span class="title">implements</span> <span class="title">OrderRepositoryInterface</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">getAll</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">'Getting all from mysql'</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">namespace App\Http\Controllers;</span><br><span class="line"></span><br><span class="line">use Illuminate\Http\Request;</span><br><span class="line">use App\Http\Requests;</span><br><span class="line">use App\Repositories\OrderRepositoryInterface;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OrdersController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $order;</span><br><span class="line"></span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">OrderRepositoryInterface $order</span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">     $<span class="keyword">this</span>-&gt;order = $order;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">   </span>&#123;</span><br><span class="line">     dd($<span class="keyword">this</span>-&gt;order-&gt;getAll());</span><br><span class="line">     <span class="keyword">return</span> View::make(orders.index);</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line">Route::resource(<span class="string">'orders'</span>, <span class="string">'OrdersController'</span>);</span><br><span class="line">App::bind(<span class="string">'App\Repositories\OrderRepositoryInterface'</span>, <span class="string">'App\Repositories\DbOrderRepository'</span>);</span><br><span class="line">定义一个容器类：</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SimpleContainer</span></span></span><br><span class="line"><span class="class"> </span>&#123;</span><br><span class="line">    protected <span class="keyword">static</span> $container = [];</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">$name, Callable $resolver</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;   </span><br><span class="line">        <span class="keyword">static</span>::$container[$name] = $resolver;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$name</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">      <span class="keyword">if</span>(isset(<span class="keyword">static</span>::$container[$name]))&#123;</span><br><span class="line">        $resolver = <span class="keyword">static</span>::$container[$name] ;</span><br><span class="line">        <span class="keyword">return</span> $resolver();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">throw</span> <span class="keyword">new</span> Exception(<span class="string">"Binding does not exist in containeer"</span>);</span><br><span class="line"></span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LogToDatabase</span> </span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">execute</span>(<span class="params">$message</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">       var_dump(<span class="string">'log the message to a database :'</span>.$message);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UsersController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    protected $logger;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">LogToDatabase $logger</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;logger = $logger;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">show</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">      $user = <span class="string">'JohnDoe'</span>;</span><br><span class="line">      $<span class="keyword">this</span>-&gt;logger-&gt;execute($user);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">绑定依赖：</span><br><span class="line"></span><br><span class="line">SimpleContainer::bind(<span class="string">'Foo'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"> </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> <span class="keyword">new</span> UsersController(<span class="keyword">new</span> LogToDatabase);</span><br><span class="line"> &#125;);</span><br><span class="line"></span><br><span class="line">$foo = SimpleContainer::make(<span class="string">'Foo'</span>);</span><br><span class="line"></span><br><span class="line">print_r($foo-&gt;show());</span><br><span class="line"></span><br><span class="line">Laravel 的服务容器源码：vendor/laravel/framwork/src/Illuminate/Container/Container.php</span><br><span class="line"></span><br><span class="line">  public <span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">$abstract, $concrete = null, $shared = false</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $abstract = $<span class="keyword">this</span>-&gt;normalize($abstract);</span><br><span class="line"></span><br><span class="line">        $concrete = $<span class="keyword">this</span>-&gt;normalize($concrete);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (is_array($abstract)) &#123;</span><br><span class="line">           list($abstract, $alias) = $<span class="keyword">this</span>-&gt;extractAlias($abstract);</span><br><span class="line"></span><br><span class="line">           $<span class="keyword">this</span>-&gt;alias($abstract, $alias);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;dropStaleInstances($abstract);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (is_null($concrete)) &#123;</span><br><span class="line">            $concrete = $abstract;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (! $concrete <span class="keyword">instanceof</span> Closure) &#123;</span><br><span class="line">            $concrete = $<span class="keyword">this</span>-&gt;getClosure($abstract, $concrete);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;bindings[$abstract] = compact(<span class="string">'concrete'</span>, <span class="string">'shared'</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;resolved($abstract)) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;rebound($abstract);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">make</span>(<span class="params">$abstract, array $parameters = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $abstract = $<span class="keyword">this</span>-&gt;getAlias($<span class="keyword">this</span>-&gt;normalize($abstract));</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;instances[$abstract])) &#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;instances[$abstract];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $concrete = $<span class="keyword">this</span>-&gt;getConcrete($abstract);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;isBuildable($concrete, $abstract)) &#123;</span><br><span class="line">            $object = $<span class="keyword">this</span>-&gt;build($concrete, $parameters);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $object = $<span class="keyword">this</span>-&gt;make($concrete, $parameters);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        foreach ($<span class="keyword">this</span>-&gt;getExtenders($abstract) <span class="keyword">as</span> $extender) &#123;</span><br><span class="line">            $object = $extender($object, $<span class="keyword">this</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;isShared($abstract)) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;instances[$abstract] = $object;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">       $<span class="keyword">this</span>-&gt;fireResolvingCallbacks($abstract, $object);</span><br><span class="line"></span><br><span class="line">       $<span class="keyword">this</span>-&gt;resolved[$abstract] = <span class="literal">true</span>;</span><br><span class="line"></span><br><span class="line">       <span class="keyword">return</span> $object;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">build</span>(<span class="params">$concrete, array $parameters = []</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($concrete <span class="keyword">instanceof</span> Closure) &#123;</span><br><span class="line">            <span class="keyword">return</span> $concrete($<span class="keyword">this</span>, $parameters);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">       $reflector = <span class="keyword">new</span> ReflectionClass($concrete);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (! $reflector-&gt;isInstantiable()) &#123;</span><br><span class="line">            <span class="keyword">if</span> (! empty($<span class="keyword">this</span>-&gt;buildStack)) &#123;</span><br><span class="line">                $previous = implode(<span class="string">', '</span>, $<span class="keyword">this</span>-&gt;buildStack);</span><br><span class="line">                $message = <span class="string">"Target [$concrete] is not instantiable while building [$previous]."</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $message = <span class="string">"Target [$concrete] is not instantiable."</span>;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">          <span class="keyword">throw</span> <span class="keyword">new</span> BindingResolutionException($message);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">         $<span class="keyword">this</span>-&gt;buildStack[] = $concrete;</span><br><span class="line"></span><br><span class="line">         $<span class="keyword">constructor</span> = $reflector-&gt;getConstructor();</span><br><span class="line"></span><br><span class="line">        if (is_null($<span class="keyword">constructor</span>)) &#123;</span><br><span class="line">            array_pop($<span class="keyword">this</span>-&gt;buildStack);</span><br><span class="line"></span><br><span class="line">           <span class="keyword">return</span> <span class="keyword">new</span> $concrete;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $dependencies = $<span class="keyword">constructor</span>-&gt;getParameters();</span><br><span class="line">        $parameters = $this-&gt;keyParametersByArgument(</span><br><span class="line">            $dependencies, $parameters</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">         $instances = $this-&gt;getDependencies($dependencies,$parameters);</span><br><span class="line"></span><br><span class="line">         array_pop($this-&gt;buildStack);</span><br><span class="line"></span><br><span class="line">         return $reflector-&gt;newInstanceArgs($instances);</span><br><span class="line">    &#125;</span><br><span class="line">    $this-&gt;app-&gt;bind('HelpSpot\API', function ($app) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> HelpSpot\API($app-&gt;make(<span class="string">'HttpClient'</span>));</span><br><span class="line">    &#125;);</span><br><span class="line">$api = <span class="keyword">new</span> HelpSpot\API(<span class="keyword">new</span> HttpClient);</span><br><span class="line"></span><br><span class="line">$<span class="keyword">this</span>-&gt;app-&gt;instance(<span class="string">'HelpSpot\API'</span>, $api);</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/26922 https://github.com/nahidulhasan/laravel-service-container</span></span><br><span class="line">Ioc 是一个简单的组件，可以更加方便地解析依赖项。你可以将对象形容为容器，并且每次解析类时，都会自动注入依赖项。</span><br><span class="line">$auth = <span class="keyword">new</span> SimpleAuth( <span class="keyword">new</span> FileSessionStorage() );</span><br><span class="line">因为 Application 类继承自 Container 类，所以你可以通过 App 门面来访问容器。</span><br><span class="line"></span><br><span class="line">App::bind( <span class="string">'FileSessionStorage'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> FileSessionStorage;</span><br><span class="line">&#125;);</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/26721</span></span><br><span class="line"></span><br><span class="line">App:bind( <span class="string">'SessionStorage'</span>, <span class="string">'MysqlSessionStorage'</span> );<span class="comment">//SessionStorage是接口,MysqlSessionStorage和FileSessionStorage继承ta，调用的时候注入SessionStorage $session </span></span><br><span class="line">如果我们想要切换我们的存储服务，我们只要变更一下这个绑定。App:bind( <span class="string">'SessionStorage'</span>, <span class="string">'FileSessionStorage '</span> );</span><br><span class="line"></span><br><span class="line">laravel面试哪些问题 v2ex</span><br></pre></td></tr></table></figure>
<h3 id="laravel函数"><a href="#laravel函数" class="headerlink" title="laravel函数"></a>laravel函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//在给定值后返回字符串的其余部分。</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">after</span>(<span class="params">$subject, $search</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        dump(explode($search, $subject, <span class="number">2</span>),array_reverse(explode($search, $subject, <span class="number">2</span>)));</span><br><span class="line">        <span class="comment">//array:2 [▼</span></span><br><span class="line">            <span class="number">0</span> =&gt; <span class="string">""</span></span><br><span class="line">            <span class="number">1</span> =&gt; <span class="string">"试laravel函数"</span></span><br><span class="line">          ]</span><br><span class="line">          array:<span class="number">2</span> [▼</span><br><span class="line">            <span class="number">0</span> =&gt; <span class="string">"试laravel函数"</span></span><br><span class="line">            <span class="number">1</span> =&gt; <span class="string">""</span></span><br><span class="line">          ]</span><br><span class="line">        <span class="keyword">return</span> $search === <span class="string">''</span> ? $subject : array_reverse(explode($search, $subject, <span class="number">2</span>))[<span class="number">0</span>];</span><br><span class="line">    &#125;</span><br><span class="line">$s = <span class="string">'测试laravel函数'</span>;echo after($s,<span class="string">'测'</span>);<span class="comment">//试laravel函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">ascii</span>(<span class="params">$value, $language = <span class="string">'en'</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $languageSpecific = languageSpecificCharsArray($language);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (! is_null($languageSpecific)) &#123;</span><br><span class="line">            $value = str_replace($languageSpecific[<span class="number">0</span>], $languageSpecific[<span class="number">1</span>], $value);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        foreach ( charsArray() <span class="keyword">as</span> $key =&gt; $val) &#123;</span><br><span class="line">            $value = str_replace($val, $key, $value);<span class="comment">//循环将数组内的字符串替换</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> preg_replace(<span class="string">'/[^\x20-\x7E]/u'</span>, <span class="string">''</span>, $value);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">languageSpecificCharsArray</span>(<span class="params">$language</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">static</span> $languageSpecific;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (! isset($languageSpecific)) &#123;</span><br><span class="line">            $languageSpecific = [</span><br><span class="line">                <span class="string">'bg'</span> =&gt; [</span><br><span class="line">                    [<span class="string">'х'</span>, <span class="string">'Х'</span>, <span class="string">'щ'</span>, <span class="string">'Щ'</span>, <span class="string">'ъ'</span>, <span class="string">'Ъ'</span>, <span class="string">'ь'</span>, <span class="string">'Ь'</span>],</span><br><span class="line">                    [<span class="string">'h'</span>, <span class="string">'H'</span>, <span class="string">'sht'</span>, <span class="string">'SHT'</span>, <span class="string">'a'</span>, <span class="string">'А'</span>, <span class="string">'y'</span>, <span class="string">'Y'</span>],</span><br><span class="line">                ],</span><br><span class="line">                <span class="string">'de'</span> =&gt; [</span><br><span class="line">                    [<span class="string">'ä'</span>,  <span class="string">'ö'</span>,  <span class="string">'ü'</span>,  <span class="string">'Ä'</span>,  <span class="string">'Ö'</span>,  <span class="string">'Ü'</span>],</span><br><span class="line">                    [<span class="string">'ae'</span>, <span class="string">'oe'</span>, <span class="string">'ue'</span>, <span class="string">'AE'</span>, <span class="string">'OE'</span>, <span class="string">'UE'</span>],</span><br><span class="line">                ],</span><br><span class="line">            ];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $languageSpecific[$language] ?? <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">charsArray</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">static</span> $charsArray;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (isset($charsArray)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $charsArray;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $charsArray = [</span><br><span class="line">            <span class="string">'0'</span>    =&gt; [<span class="string">'°'</span>, <span class="string">'₀'</span>, <span class="string">'۰'</span>, <span class="string">'０'</span>],</span><br><span class="line">            <span class="string">'1'</span>    =&gt; [<span class="string">'¹'</span>, <span class="string">'₁'</span>, <span class="string">'۱'</span>, <span class="string">'１'</span>],</span><br><span class="line">            <span class="string">'2'</span>    =&gt; [<span class="string">'²'</span>, <span class="string">'₂'</span>, <span class="string">'۲'</span>, <span class="string">'２'</span>],</span><br><span class="line">            <span class="string">'3'</span>    =&gt; [<span class="string">'³'</span>, <span class="string">'₃'</span>, <span class="string">'۳'</span>, <span class="string">'３'</span>],</span><br><span class="line">            <span class="string">'4'</span>    =&gt; [<span class="string">'⁴'</span>, <span class="string">'₄'</span>, <span class="string">'۴'</span>, <span class="string">'٤'</span>, <span class="string">'４'</span>],</span><br><span class="line">            <span class="string">'5'</span>    =&gt; [<span class="string">'⁵'</span>, <span class="string">'₅'</span>, <span class="string">'۵'</span>, <span class="string">'٥'</span>, <span class="string">'５'</span>],</span><br><span class="line">            <span class="string">'6'</span>    =&gt; [<span class="string">'⁶'</span>, <span class="string">'₆'</span>, <span class="string">'۶'</span>, <span class="string">'٦'</span>, <span class="string">'６'</span>],</span><br><span class="line">            <span class="string">'7'</span>    =&gt; [<span class="string">'⁷'</span>, <span class="string">'₇'</span>, <span class="string">'۷'</span>, <span class="string">'７'</span>],</span><br><span class="line">            <span class="string">'8'</span>    =&gt; [<span class="string">'⁸'</span>, <span class="string">'₈'</span>, <span class="string">'۸'</span>, <span class="string">'８'</span>],</span><br><span class="line">            <span class="string">'9'</span>    =&gt; [<span class="string">'⁹'</span>, <span class="string">'₉'</span>, <span class="string">'۹'</span>, <span class="string">'９'</span>],</span><br><span class="line">            &#125;</span><br><span class="line"> 将utf - <span class="number">8</span>值转换为ASCII           </span><br><span class="line">ascii($s);<span class="comment">//laravel</span></span><br><span class="line"><span class="comment">//在给定值之前获取字符串的部分。</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">before</span>(<span class="params">$subject, $search</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $search === <span class="string">''</span> ? $subject : explode($search, $subject)[<span class="number">0</span>];</span><br><span class="line">    &#125;</span><br><span class="line"> <span class="comment">//将一个值转换为一个大写的大写字母</span></span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">studly</span>(<span class="params">$value</span>)</span></span><br><span class="line"><span class="function">         </span>&#123;</span><br><span class="line">                 $key = $value;</span><br><span class="line"> </span><br><span class="line">                 <span class="keyword">if</span> (isset(<span class="keyword">static</span>::$studlyCache[$key])) &#123;</span><br><span class="line">                         <span class="keyword">return</span> <span class="keyword">static</span>::$studlyCache[$key];</span><br><span class="line">                 &#125;</span><br><span class="line"> </span><br><span class="line">                 $value = ucwords(str_replace([<span class="string">'-'</span>, <span class="string">'_'</span>], <span class="string">' '</span>, $value));</span><br><span class="line"> </span><br><span class="line">                 <span class="keyword">return</span> <span class="keyword">static</span>::$studlyCache[$key] = str_replace(<span class="string">' '</span>, <span class="string">''</span>, $value);</span><br><span class="line">         &#125;</span><br></pre></td></tr></table></figure>
<h3 id="路由为不区分大小写"><a href="#路由为不区分大小写" class="headerlink" title="路由为不区分大小写"></a>路由为不区分大小写</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">添加中间件. LowerCaseRoutes</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace App\Http\Middleware;</span><br><span class="line"></span><br><span class="line">use Closure;</span><br><span class="line">use \Illuminate\Support\Facades\Redirect;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 把大写uri转换为小写.已经实现路由不区分大小写，如果有大写，会 301 跳转到小写的地址.</span></span><br><span class="line"><span class="comment">               </span></span><br><span class="line"><span class="comment">https://learnku.com/laravel/t/27783</span></span><br><span class="line"><span class="comment"> * Class LowerCaseRoutes</span></span><br><span class="line"><span class="comment"> * @package App\Http\Middleware</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LowerCaseRoutes</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Handle an incoming request.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @param  \Illuminate\Http\Request  $request</span></span><br><span class="line"><span class="comment">     * @param  \Closure  $next</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">$request, Closure $next</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (preg_match(<span class="string">'/([A-Z]+)/'</span>, $request-&gt;path(), $match)) &#123;</span><br><span class="line">            $newRoute = str_replace($request-&gt;path(), strtolower($request-&gt;path()), $request-&gt;fullUrl());</span><br><span class="line">            <span class="keyword">return</span> Redirect::to($newRoute, <span class="number">301</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $next($request);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="数组扁平化"><a href="#数组扁平化" class="headerlink" title="数组扁平化"></a>数组扁平化</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">$data = [</span><br><span class="line">    <span class="string">'testArray'</span> =&gt; [</span><br><span class="line">        <span class="string">'string1'</span>,</span><br><span class="line">        <span class="string">'string2'</span>,</span><br><span class="line">        [</span><br><span class="line">            <span class="string">'string1'</span>,</span><br><span class="line">            <span class="string">'string2'</span>,</span><br><span class="line">        ],</span><br><span class="line">    ],</span><br><span class="line">    <span class="string">'teststring'</span></span><br><span class="line">];</span><br><span class="line">echo <span class="string">"&lt;pre&gt;"</span>;https:<span class="comment">//learnku.com/articles/24318</span></span><br><span class="line"></span><br><span class="line">print_r(iterator_to_array(</span><br><span class="line">        <span class="keyword">new</span> RecursiveIteratorIterator(</span><br><span class="line">            <span class="keyword">new</span> RecursiveArrayIterator($data)</span><br><span class="line">        ),</span><br><span class="line">        <span class="literal">false</span>  <span class="comment">// use_keys:默认true</span></span><br><span class="line">    )</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">/*Array</span></span><br><span class="line"><span class="comment">(</span></span><br><span class="line"><span class="comment">    [0] =&gt; string1</span></span><br><span class="line"><span class="comment">    [1] =&gt; string2</span></span><br><span class="line"><span class="comment">    [2] =&gt; string1</span></span><br><span class="line"><span class="comment">    [3] =&gt; string2</span></span><br><span class="line"><span class="comment">    [4] =&gt; teststring</span></span><br><span class="line"><span class="comment">)*/</span></span><br><span class="line">collect ($data)-&gt;flatten ()-&gt;all ()</span><br></pre></td></tr></table></figure>
<h3 id="phpexcel科学计数法"><a href="#phpexcel科学计数法" class="headerlink" title="phpexcel科学计数法"></a>phpexcel科学计数法</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">拼一个 \t</span><br><span class="line">设置单元格格式为文本格式https:<span class="comment">//learnku.com/articles/23696</span></span><br><span class="line"></span><br><span class="line">          $card_no_style = [</span><br><span class="line">                        <span class="string">'numberFormat'</span> =&gt; [</span><br><span class="line">                            <span class="string">'formatCode'</span> =&gt; NumberFormat::FORMAT_TEXT</span><br><span class="line">                        ]</span><br><span class="line"></span><br><span class="line">                    ];</span><br><span class="line">                    $obj_sheet-&gt;getStyle(Coordinate::stringFromColumnIndex($column) . <span class="string">"1"</span> . <span class="string">":"</span> . Coordinate::stringFromColumnIndex($column) . count($data))-&gt;applyFromArray($card_no_style);</span><br></pre></td></tr></table></figure>
<h3 id="PHPExcel-在-PHP7-0-以上版本报错"><a href="#PHPExcel-在-PHP7-0-以上版本报错" class="headerlink" title="PHPExcel 在 PHP7.0 以上版本报错"></a>PHPExcel 在 PHP7.0 以上版本报错</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'break'</span> not <span class="keyword">in</span> the <span class="string">'loop'</span> or <span class="string">'switch'</span> context</span><br><span class="line"></span><br><span class="line">PHPExcel\Calculation\Functions.php 　LINE: <span class="number">581</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (($value === NULL) || (is_float($value)) || (is_int($value))) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125; elseif(is_bool($value)) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">4</span>;</span><br><span class="line">        &#125; elseif(is_array($value)) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">64</span>;</span><br><span class="line">                <span class="keyword">break</span>;<span class="comment">//这是581行</span></span><br><span class="line">        &#125; elseif(is_string($value)) &#123;</span><br><span class="line">            <span class="comment">//  Errors</span></span><br><span class="line">            <span class="keyword">if</span> ((strlen($value) &gt; <span class="number">0</span>) &amp;&amp; ($value&#123;<span class="number">0</span>&#125; == <span class="string">'#'</span>)) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">16</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">         <span class="keyword">return</span> 跳出当前函数或者循环直接返回 <span class="keyword">break</span> 并会执行而报错https:<span class="comment">//learnku.com/articles/26675</span></span><br><span class="line">        </span><br><span class="line">        方案 <span class="number">1</span>: 将 <span class="number">581</span> 行的 <span class="keyword">break</span> 注释掉</span><br><span class="line">        </span><br><span class="line">        方案 <span class="number">2</span> : 将 PHPExcel 升级到 <span class="number">1.81</span> 版本以上</span><br><span class="line">        </span><br><span class="line">        方案三：使用 PHPExcel 的升级产品 PhpSpreadsheet</span><br><span class="line">        </span><br><span class="line">        方案 <span class="number">1</span> 修改源码 不推荐</span><br><span class="line">        方案 <span class="number">2</span>PHPExcel 已不再维护 故不推荐</span><br><span class="line">        对于框架版本国过老 或经多手的项目 建议使用 方案 <span class="number">1</span>,<span class="number">2</span></span><br><span class="line">        </span><br><span class="line">        方案 <span class="number">3</span> 为 PHPExcel 的替代品升级产品 而且在维护 新项目推荐方案 <span class="number">3</span></span><br><span class="line">        </span><br><span class="line">        PHPExcel <span class="number">1.8</span><span class="number">.1</span> 地址 https:<span class="comment">//github.com/PHPOffice/PHPExcel/commit/372c7cbb695a6f6f1e62649381aeaa37e7e70b32</span></span><br><span class="line">        </span><br><span class="line">        PhpSpreadsheet 安装 如下</span><br><span class="line">        </span><br><span class="line">        文档地址：https:<span class="comment">//phpspreadsheet.readthedocs.io/en/latest/#getting-started</span></span><br><span class="line">        </span><br><span class="line">        GitHub 下载：https:<span class="comment">//github.com/PHPOffice/PhpSpreadsheet</span></span><br><span class="line">        </span><br><span class="line">        composer 安装：composer <span class="built_in">require</span> phpoffice/phpspreadsheet</span><br><span class="line">        </span><br><span class="line">        PHPExcel 与 PhpSpreadsheet？</span><br><span class="line">        </span><br><span class="line">        PhpSpreadsheet 是 PHPExcel 的下一个版本。它打破了兼容性，大大提高了代码库质量（命名空间，PSR 合规性，最新 PHP 语言功能的使用等）。</span><br><span class="line">        </span><br><span class="line">        因为所有的努力都转移到了 PhpSpreadsheet，所以不再维护 PHPExcel。PHPExcel，补丁和新功能的所有贡献都应该以 PhpSpreadsheet master 分支为目标。</span><br></pre></td></tr></table></figure>
<h3 id="foreach"><a href="#foreach" class="headerlink" title="foreach"></a>foreach</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">$data=array(<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>);</span><br><span class="line">  foreach($data <span class="keyword">as</span> $key=&gt;$value)&#123;</span><br><span class="line">       $value=&amp;$data[$key];</span><br><span class="line">  &#125;</span><br><span class="line">问题<span class="number">1</span>:程序执行时，每一次循环结束后变量$data的值是什么？请解释。</span><br><span class="line">问题<span class="number">2</span>:程序执行完后，变量$data的值是什么？请解释。</span><br><span class="line"><span class="comment">//第一次遍历 结果还是 【a,b,c】</span></span><br><span class="line"><span class="comment">//第二次遍历</span></span><br><span class="line">$data[<span class="number">0</span>]=<span class="string">'a'</span>;<span class="comment">//第一次遍历的时候$data[0]=a</span></span><br><span class="line">$val=&amp;$data[<span class="number">0</span>];<span class="comment">//这边已经把$data[0]引用指向$val 第一次遍历以后</span></span><br><span class="line">$val=b <span class="comment">//现在$val 一旦变了 那么$data[0]也就改变了 所以变成$data[0]=b</span></span><br><span class="line">$data=[b,b,c];<span class="comment">//最后结果变成了</span></span><br><span class="line"><span class="comment">//第三次遍历</span></span><br><span class="line">$data[<span class="number">1</span>]=b;<span class="comment">//第二次遍历的时候$data[1]变成了b</span></span><br><span class="line">$val=&amp;$data[<span class="number">1</span>];<span class="comment">//第二次遍历的时候$val的引用变成了$data[1]</span></span><br><span class="line">$val=c;<span class="comment">//所以这边$val变成了c 那么$data[1]也就变成了c</span></span><br><span class="line">$data=[<span class="string">'b'</span>,<span class="string">'c'</span>,<span class="string">'c'</span>]<span class="comment">//最后结果变成了</span></span><br><span class="line"><span class="comment">//最后的$data的结果https://learnku.com/articles/26374</span></span><br><span class="line">【b,c,c】</span><br><span class="line">list($b, $a) = array($a, $b);</span><br></pre></td></tr></table></figure>
<h3 id="tap"><a href="#tap" class="headerlink" title="tap"></a>tap</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">$items = [</span><br><span class="line">    [<span class="string">'name'</span> =&gt; <span class="string">'David Charleston'</span>, <span class="string">'member'</span> =&gt; <span class="number">1</span>, <span class="string">'active'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">    [<span class="string">'name'</span> =&gt; <span class="string">'Blain Charleston'</span>, <span class="string">'member'</span> =&gt; <span class="number">0</span>, <span class="string">'active'</span> =&gt; <span class="number">0</span>],</span><br><span class="line">    [<span class="string">'name'</span> =&gt; <span class="string">'Megan Tarash'</span>, <span class="string">'member'</span> =&gt; <span class="number">1</span>, <span class="string">'active'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">    [<span class="string">'name'</span> =&gt; <span class="string">'Jonathan Phaedrus'</span>, <span class="string">'member'</span> =&gt; <span class="number">1</span>, <span class="string">'active'</span> =&gt; <span class="number">1</span>],</span><br><span class="line">    [<span class="string">'name'</span> =&gt; <span class="string">'Paul Jackson'</span>, <span class="string">'member'</span> =&gt; <span class="number">0</span>, <span class="string">'active'</span> =&gt; <span class="number">1</span>]</span><br><span class="line">];</span><br><span class="line">先将数组转为集合，再进行数据的过滤，最后在两个不同的节点调用 tap 方法，返回打印结果：</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> collect($items)</span><br><span class="line">    -&gt;where(<span class="string">'active'</span>, <span class="number">1</span>)</span><br><span class="line">    -&gt;tap(<span class="function"><span class="keyword">function</span>(<span class="params">$collection</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> var_dump($collection-&gt;pluck(<span class="string">'name'</span>));</span><br><span class="line">    &#125;)</span><br><span class="line">    -&gt;where(<span class="string">'member'</span>, <span class="number">1</span>)</span><br><span class="line">    -&gt;tap(<span class="function"><span class="keyword">function</span>(<span class="params">$collection</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> var_dump($collection-&gt;pluck(<span class="string">'name'</span>));</span><br><span class="line">    &#125;);</span><br><span class="line">第一次调用 tap 方法输出结果：https:<span class="comment">//learnku.com/laravel/t/26361</span></span><br><span class="line"></span><br><span class="line">David Charleston, Megan Tarash, Jonathan Phaedrus, Paul Jackson</span><br><span class="line">第二次调用 tap 方法输出结果：</span><br><span class="line"></span><br><span class="line">David Charleston, Megan Tarash, Jonathan Phaedrus</span><br><span class="line">Tap vs Pipe</span><br><span class="line"></span><br><span class="line">Laravel 也提供了另一个类似 tap 的集合操作方法 -- pipe，两者在集合调用上很类似，却有一个主要的区别：</span><br><span class="line"></span><br><span class="line">通过调用 tap 方法不会改变原集合的结果，而 pipe 方法会根据返回值修改元集合的结果。示例如下：</span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> collect($items)</span><br><span class="line">    -&gt;where(<span class="string">'active'</span>, <span class="number">1</span>)</span><br><span class="line">    -&gt;pipe(<span class="function"><span class="keyword">function</span> (<span class="params">$collection</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $collection-&gt;push([<span class="string">'name'</span> =&gt; <span class="string">'John Doe'</span>]);</span><br><span class="line">    &#125;);</span><br><span class="line"><span class="comment">// David Charleston, Megan Tarash, Jonathan Phaedrus, Paul Jackson, John Doe</span></span><br></pre></td></tr></table></figure>
<h3 id="artisan日志-root-权限解决"><a href="#artisan日志-root-权限解决" class="headerlink" title="artisan日志 root 权限解决"></a>artisan日志 root 权限解决</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">bootstrap/app.php 中写入以下配置： </span><br><span class="line">https:<span class="comment">//learnku.com/articles/26444</span></span><br><span class="line">$app-&gt;configureMonologUsing(<span class="function"><span class="keyword">function</span>(<span class="params">Monolog\Logger $monolog</span>) </span>&#123;</span><br><span class="line">    $filename = storage_path(<span class="string">'logs/laravel-'</span>.php_sapi_name().<span class="string">'.log'</span>);</span><br><span class="line">    $handler = <span class="keyword">new</span> Monolog\Handler\RotatingFileHandler($filename);</span><br><span class="line">    $monolog-&gt;pushHandler($handler);</span><br><span class="line">&#125;);</span><br><span class="line">正确的做法应该是用 www 用户执行 artisan 命令，因为不光是日志，还有可能有缓存、 storage 等文件可能被 root 用户创建而导致 www 用户无权操作。</span><br><span class="line">脚本写在 crontab 中的，如果我想用 www 用户执行 artisan 命令，我应该怎么写 crontab 呢 crontab -e -u www</span><br><span class="line">php cli 或者 fpm 都是用 www 用户去执行 就不会出现你这个问题的</span><br><span class="line">不想使用单独的用户配置 crontab，也可以这么干，比如：配置在 root 账号下</span><br><span class="line"></span><br><span class="line">su www -s /bin/bash -c “php artisan schedule:run”</span><br></pre></td></tr></table></figure>
<h3 id="英文字符占-0-5-个，中文字符占-1-个"><a href="#英文字符占-0-5-个，中文字符占-1-个" class="headerlink" title="英文字符占 0.5 个，中文字符占 1 个"></a>英文字符占 0.5 个，中文字符占 1 个</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/articles/4956/following-my-unique-needs-of-the-english-characters-accounted-for-05-chinese-characters-accounted-for-1#topnav</span></span><br><span class="line">$double = str_word_count(preg_replace(<span class="string">'([a-zA-Z0-9_])'</span>, <span class="string">''</span>, $value));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 计算单字节.</span></span><br><span class="line">    preg_match_all(<span class="string">'/[a-zA-Z0-9_]/'</span>, $value, $single);</span><br><span class="line">    $single = count($single[<span class="number">0</span>]) / <span class="number">2</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 多子节长度.</span></span><br><span class="line">    $double = mb_strlen(preg_replace(<span class="string">'([a-zA-Z0-9_])'</span>, <span class="string">''</span>, $value));</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">the_fucking_length_function</span>(<span class="params">$str</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> strlen(mb_convert_encoding($str, <span class="string">'GB2312'</span>)) / <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line">$length = (strlen($value) + mb_strlen($value)) / <span class="number">2</span>;</span><br><span class="line"><span class="comment">//这个方法是好的，缺点就只有一个，就是精度问题，因为 utf-8 中只有两万多个汉字是 3 字节，还有六万多的不常用汉字是 4 字节的，附带 emoji 之类的就</span></span><br><span class="line"> <span class="function"><span class="keyword">function</span> <span class="title">mbstrlen</span>(<span class="params">$str</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> ceil((strlen($str) + mb_strlen($str, <span class="string">"UTF8"</span>)) / <span class="number">2</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">str_display_len</span> (<span class="params">string $str</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    preg_match_all(<span class="string">'/[a-zA-Z0-9_]/'</span>, $str, $single);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> count($single[<span class="number">0</span>]) / <span class="number">2</span> + mb_strlen(preg_replace(<span class="string">'([a-zA-Z0-9_])'</span>, <span class="string">''</span>, $str));</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//www.php.net/manual/zh/function.mb-strwidth.php</span></span><br></pre></td></tr></table></figure>
<h3 id="bas64简历"><a href="#bas64简历" class="headerlink" title="bas64简历"></a>bas64简历</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/laravel/t/25907</span></span><br></pre></td></tr></table></figure>
<h3 id="PHP-排名算法支持重复排名"><a href="#PHP-排名算法支持重复排名" class="headerlink" title="PHP 排名算法支持重复排名"></a>PHP 排名算法支持重复排名</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://learnku.com/articles/26769</span></span><br><span class="line">$arr = [</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">1</span>,</span><br><span class="line">        <span class="string">'score'</span>=&gt;<span class="number">10</span>,</span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">2</span>,</span><br><span class="line">        <span class="string">'score'</span>=&gt;<span class="number">30</span>,</span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">3</span>,</span><br><span class="line">        <span class="string">'score'</span>=&gt;<span class="number">50</span>,</span><br><span class="line">    ],</span><br><span class="line">    [</span><br><span class="line">        <span class="string">'id'</span>=&gt;<span class="number">4</span>,</span><br><span class="line">        <span class="string">'score'</span>=&gt;<span class="number">50</span>,</span><br><span class="line">    ]</span><br><span class="line">]</span><br><span class="line">你想从小到大，取出前三名，如果第四名和第三名也相同，也取出来</span><br><span class="line"></span><br><span class="line">先按 score 排序</span><br><span class="line"></span><br><span class="line">array_multisort(array_column($arr,<span class="string">'score'</span>),SORT_ASC,$arr);</span><br><span class="line">前三名</span><br><span class="line"></span><br><span class="line"> $results = array_slice($arr, <span class="number">0</span>, <span class="number">3</span>, <span class="literal">true</span>);</span><br><span class="line">然后前三名的最后一名和之后的比较</span><br><span class="line"></span><br><span class="line">$length = count($arr);</span><br><span class="line">$resultLength = count($results);</span><br><span class="line"></span><br><span class="line"><span class="comment">//$resultLength 比 $length小说明$arr 的还有元素，否则它们俩就相等了。然后比较key和key+1的值。</span></span><br><span class="line">极限是所有的人都和第三名相同，自行判断是否需要所有人都胜出，来限制 $resultLength 的长度</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span>($resultLength&lt;$length &amp;&amp;$results[$resultLength<span class="number">-1</span>][<span class="string">'score'</span>]==$arr[$resultLength][<span class="string">'score'</span>])&#123;</span><br><span class="line">            array_push($results,$arr[$resultLength]);</span><br><span class="line">            $resultLength = count($results);</span><br><span class="line">        &#125;</span><br><span class="line">最后如果要判断某一用户是否胜出</span><br><span class="line"></span><br><span class="line">$ifWinner = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">if</span>(in_array($id,array_column($results,<span class="string">'id'</span>)))&#123;</span><br><span class="line">       $ifWinner = <span class="number">1</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="多个-Laravel-应用-queue-队列执行时会互串"><a href="#多个-Laravel-应用-queue-队列执行时会互串" class="headerlink" title="多个 Laravel 应用 queue 队列执行时会互串"></a>多个 Laravel 应用 queue 队列执行时会互串</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line">app/Libriries/helper.php ，可以写在你任何想放的地方</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 多个 laravel 项目，如果 --queue 相同会互串</span></span><br><span class="line"><span class="comment"> * 这里统一加个当前项目名的前缀来区分</span></span><br><span class="line"><span class="comment"> * @param $name</span></span><br><span class="line"><span class="comment"> * @return string</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">queue_name</span>(<span class="params">$name</span>)</span>&#123;</span><br><span class="line">    $name_trans = [];</span><br><span class="line">    foreach(explode(<span class="string">','</span>,$name) <span class="keyword">as</span> $k =&gt; $v)&#123;</span><br><span class="line">        $name_trans[] = str_slug(config(<span class="string">'app.name'</span>) . <span class="string">' '</span> . $v, <span class="string">'_'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> implode(<span class="string">','</span>,$name_trans);</span><br><span class="line">&#125;</span><br><span class="line">修改每个 Job 的 $queue 添加 queue_name ()</span><br><span class="line"></span><br><span class="line">namespace App\Jobs;</span><br><span class="line"><span class="comment">// 省略...</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ApiBehavior</span> <span class="title">implements</span> <span class="title">ShouldQueue</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 添加这一行</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;queue = queue_name(<span class="string">'你的队列名'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 分发队列时，照常分发即可</span></span><br><span class="line">ApiBehavior::dispatch()</span><br><span class="line"><span class="comment">// 或者这种分发时指定，可以省略第二步。</span></span><br><span class="line">ApiBehavior::dispatch()-&gt;onQueue(quque_name(<span class="string">'你的队列名'</span>))</span><br><span class="line">新建监听队列命令，包装一层 quque:work，以使用自定义队列名</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace App\Console\Commands;</span><br><span class="line"></span><br><span class="line">use Illuminate\Console\Command;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">QueueWorkListen</span> <span class="keyword">extends</span> <span class="title">Command</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The name and signature of the console command.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @var string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected $signature = <span class="string">'queue:work-listen</span></span><br><span class="line"><span class="string">                            &#123;connection? : The name of the queue connection to work&#125;</span></span><br><span class="line"><span class="string">                            &#123;--queue= : The names of the queues to work&#125;</span></span><br><span class="line"><span class="string">                            &#123;--daemon : Run the worker in daemon mode (Deprecated)&#125;</span></span><br><span class="line"><span class="string">                            &#123;--once : Only process the next job on the queue&#125;</span></span><br><span class="line"><span class="string">                            &#123;--delay=0 : The number of seconds to delay failed jobs&#125;</span></span><br><span class="line"><span class="string">                            &#123;--force : Force the worker to run even in maintenance mode&#125;</span></span><br><span class="line"><span class="string">                            &#123;--memory=128 : The memory limit in megabytes&#125;</span></span><br><span class="line"><span class="string">                            &#123;--sleep=3 : Number of seconds to sleep when no job is available&#125;</span></span><br><span class="line"><span class="string">                            &#123;--timeout=60 : The number of seconds a child process can run&#125;</span></span><br><span class="line"><span class="string">                            &#123;--tries=0 : Number of times to attempt a job before logging it failed&#125;'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The console command description.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @var string</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected $description = <span class="string">'Start processing jobs on the queue as a daemon'</span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Create a new command instance.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        parent::__construct();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * Execute the console command.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @return mixed</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;call(<span class="string">'queue:work'</span>,[</span><br><span class="line">            <span class="string">'connection'</span>=&gt; $<span class="keyword">this</span>-&gt;argument(<span class="string">'connection'</span>),</span><br><span class="line">            <span class="string">'--queue'</span>   =&gt; queue_name($<span class="keyword">this</span>-&gt;option(<span class="string">'queue'</span>) ?: <span class="string">'default'</span>),</span><br><span class="line">            <span class="string">'--daemon'</span>  =&gt; $<span class="keyword">this</span>-&gt;option(<span class="string">'daemon'</span>),</span><br><span class="line">            <span class="string">'--once'</span>    =&gt; $<span class="keyword">this</span>-&gt;option(<span class="string">'once'</span>),</span><br><span class="line">            <span class="string">'--delay'</span>   =&gt; $<span class="keyword">this</span>-&gt;option(<span class="string">'delay'</span>),</span><br><span class="line">            <span class="string">'--force'</span>   =&gt; $<span class="keyword">this</span>-&gt;option(<span class="string">'force'</span>),</span><br><span class="line">            <span class="string">'--memory'</span>  =&gt; $<span class="keyword">this</span>-&gt;option(<span class="string">'memory'</span>),</span><br><span class="line">            <span class="string">'--sleep'</span>   =&gt; $<span class="keyword">this</span>-&gt;option(<span class="string">'sleep'</span>),</span><br><span class="line">            <span class="string">'--timeout'</span> =&gt; $<span class="keyword">this</span>-&gt;option(<span class="string">'timeout'</span>),</span><br><span class="line">            <span class="string">'--tries'</span>   =&gt; $<span class="keyword">this</span>-&gt;option(<span class="string">'tries'</span>),</span><br><span class="line">        ]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">5</span>、使用自定义命令监听队列，其他参数与 queue:work 一样使用https:<span class="comment">//learnku.com/articles/19460</span></span><br><span class="line"></span><br><span class="line">php /<span class="keyword">var</span>/www/ebank/artisan queue:work-listen --queue=email --sleep=<span class="number">3</span></span><br><span class="line"><span class="comment">// or 多个</span></span><br><span class="line">php /<span class="keyword">var</span>/www/ebank/artisan queue:work-listen --queue=email,test,test2,test3 --sleep=<span class="number">3</span></span><br><span class="line"></span><br><span class="line">redis配置不同库就可以</span><br></pre></td></tr></table></figure>
<h3 id="修改器"><a href="#修改器" class="headerlink" title="修改器"></a>修改器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">namespace App\Models;</span><br><span class="line"></span><br><span class="line">use Illuminate\Database\Eloquent\Model;</span><br><span class="line">use Carbon\Carbon;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Baby</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $table = <span class="string">'baby'</span>;</span><br><span class="line">    protected $appends = [<span class="string">'age'</span>];</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getAgeAttribute</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $date = <span class="keyword">new</span> Carbon($<span class="keyword">this</span>-&gt;birthday);</span><br><span class="line">        <span class="keyword">return</span> Carbon::now()-&gt;diffInYears($date);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">这个代码比较简单，就是通过已有属性 birthday，计算 Baby 几岁了，得到 age 属性。</span><br><span class="line"></span><br><span class="line">前端就可以直接拿到结果：https:<span class="comment">//learnku.com/articles/21982#topnav</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">return</span> $baby-&gt;age;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">extends</span> <span class="title">Model</span> </span>&#123;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 更改为日期类型的属性</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @var array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected $dates = [</span><br><span class="line">        <span class="string">'created_at'</span>, </span><br><span class="line">        <span class="string">'updated_at'</span>, </span><br><span class="line">        <span class="string">'expires_at'</span>, </span><br><span class="line">        <span class="string">'gets_mad_at'</span></span><br><span class="line">    ];</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 我们检索时始终将名字大写</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getFirstNameAttribute</span>(<span class="params">$value</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> ucfirst($value);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 我们检索时始终将姓氏大写</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getLastNameAttribute</span>(<span class="params">$value</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> ucfirst($value);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 将名称保存到数据库时，始终将名字大写</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">setFirstNameAttribute</span>(<span class="params">$value</span>) </span>&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;attributes[<span class="string">'first_name'</span>] = ucfirst($value);</span><br><span class="line">        &#125;</span><br><span class="line">    </span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 将名称保存到数据库时，始终将姓氏大写</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        public <span class="function"><span class="keyword">function</span> <span class="title">setLastNameAttribute</span>(<span class="params">$value</span>) </span>&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;attributes[<span class="string">'last_name'</span>] = ucfirst($value);</span><br><span class="line">        &#125;</span><br><span class="line">         <span class="comment">/**</span></span><br><span class="line"><span class="comment">             * 我们将密码保存至数据库时，总是哈希后保存。</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            public <span class="function"><span class="keyword">function</span> <span class="title">setPasswordAttribute</span>(<span class="params">$value</span>) </span>&#123;</span><br><span class="line">                $<span class="keyword">this</span>-&gt;attributes[<span class="string">'password'</span>] = Hash::make($value);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">/**</span></span><br><span class="line"><span class="comment">                 * 总是 json_decode settings字段，所以它们是可用的</span></span><br><span class="line"><span class="comment">                 */</span></span><br><span class="line">                public <span class="function"><span class="keyword">function</span> <span class="title">getSettingsAttribute</span>(<span class="params">$value</span>) </span>&#123;</span><br><span class="line">                    <span class="keyword">return</span> json_decode($value);</span><br><span class="line">            </span><br><span class="line">                    <span class="comment">// 你可以确保同时返回一个数组</span></span><br><span class="line">                    <span class="comment">// return json_decode($value, true);</span></span><br><span class="line">                &#125;</span><br><span class="line">            </span><br><span class="line">                <span class="comment">/**</span></span><br><span class="line"><span class="comment">                * 保存到数据库时，总是对 settings 字段进行 json_encode</span></span><br><span class="line"><span class="comment">                 * Always json_encode the settings when saving to the database</span></span><br><span class="line"><span class="comment">                 */</span></span><br><span class="line">                public <span class="function"><span class="keyword">function</span> <span class="title">setSettingsAttribute</span>(<span class="params">$value</span>) </span>&#123;</span><br><span class="line">                    $<span class="keyword">this</span>-&gt;attributes[<span class="string">'settings'</span>] = json_encode($value);</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 获取 awesomedudette 用户  https://learnku.com/laravel/t/27551</span></span><br><span class="line">$user = App\User::where(<span class="string">'username'</span>, <span class="string">'awesomedudette'</span>)-&gt;first();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 查找此用户的过期时间前一小时</span></span><br><span class="line">$explodeWarning = $user-&gt;explodes_at-&gt;subHour();</span><br><span class="line">$user = App\User::create([</span><br><span class="line">    <span class="string">'first_name'</span>  =&gt; <span class="string">'chris'</span>,</span><br><span class="line">    <span class="string">'last_name'</span>   =&gt; <span class="string">'sevilleja'</span>,</span><br><span class="line">    <span class="string">'email'</span>       =&gt; <span class="string">'chris&amp;64;scotch.io'</span>,</span><br><span class="line">    <span class="string">'password'</span>    =&gt; <span class="string">'password'</span>,</span><br><span class="line">    <span class="string">'expires_at'</span>  =&gt; Carbon::now()-&gt;addMonth(),</span><br><span class="line">    <span class="string">'explodes_at'</span> =&gt; Carbon::now()-&gt;addDay(),</span><br><span class="line">    <span class="string">'gets_mad_at'</span> =&gt; Carbon::yesterday()</span><br><span class="line">]);</span><br></pre></td></tr></table></figure>
<h3 id="响应宏原理"><a href="#响应宏原理" class="headerlink" title="响应宏原理"></a>响应宏原理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">Response::macro(<span class="string">'success'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$data = [], $message = <span class="string">'success'</span></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> JsonResponse([</span><br><span class="line">        <span class="string">'code'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">        <span class="string">'data'</span> =&gt; $data,</span><br><span class="line">        <span class="string">'message'</span> =&gt; $message</span><br><span class="line">    ], <span class="number">200</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">接下来， 你可以再任何地方使用它response()。https:<span class="comment">//godruoyi.com/posts/laravel-response-macro-principle</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//UserController.php</span></span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">users</span>(<span class="params">UserRepository $userRepository</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> response()-&gt;success($userRepository-&gt;all(), <span class="string">'success'</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">注意，你只能通过response()这个全局方法或是app(<span class="string">'Illuminate\Routing\ResponseFactory'</span>)来使用它</span><br><span class="line"></span><br><span class="line">response()-&gt;success();<span class="comment">//OK</span></span><br><span class="line">app(<span class="string">'Illuminate\Routing\ResponseFactory'</span>)-&gt;success();<span class="comment">//OK</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//Response Facade</span></span><br><span class="line">Response::success();<span class="comment">//ok</span></span><br><span class="line"></span><br><span class="line">(<span class="keyword">new</span> \Illuminate\Http\Response)-&gt;success();<span class="comment">//Error</span></span><br><span class="line">每一次请求结束，PHP 的变量都会 unset，Laravel 的 singleton 只是在某一次请求过程中的singleton；你在 Laravel 中的静态变量也不能在多个请求之间共享，因为每一次请求结束都会 unset。理解这些概念，是写高质量代码的第一步，也是最关键的一步。因此记住，PHP是一种脚本语言，所有的变量只会在这一次请求中生效，下次请求之时已被重置，而不像Java静态变量拥有全局作用。</span><br></pre></td></tr></table></figure>
<h3 id="队列优先级的一个坑"><a href="#队列优先级的一个坑" class="headerlink" title="队列优先级的一个坑"></a>队列优先级的一个坑</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">php artisan queue:work --queue=high,low</span><br><span class="line">这样，当我们的任务需要优先发送时，就可以通过指定队列名high来优先发送。</span><br><span class="line"></span><br><span class="line">dispatch((<span class="keyword">new</span> Job)-&gt;onQueue(<span class="string">'high'</span>));</span><br><span class="line">但是当你后续任务没有指定队列名（high、low）时，你的队列任务永远也不会执行。（比如我们在发送消息通知时）</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace App\Notifications;</span><br><span class="line"></span><br><span class="line">use Illuminate\Bus\Queueable;</span><br><span class="line">use Illuminate\Notifications\Notification;</span><br><span class="line">use Illuminate\Contracts\Queue\ShouldQueue;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">YourNotification</span> <span class="keyword">extends</span> <span class="title">Notification</span> <span class="title">implements</span> <span class="title">ShouldQueue</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    use Queueable;</span><br><span class="line">&#125;</span><br><span class="line">你发现即使你按照文档说的，implements ShouldQueue并且use Queueable，该通知还是无法加入队列。</span><br><span class="line"></span><br><span class="line">那是因为config\queuq.php配置中，指定了默认的队列名为<span class="keyword">default</span>，所以所有的队列任务，如果没指定队列名时，默认是<span class="keyword">default</span>。</span><br><span class="line"></span><br><span class="line">但是我们在启动队列进程时，只指定了high和low。当然不会生效。https:<span class="comment">//godruoyi.com/posts/a-pit-in-the-laravel-queue-priority</span></span><br><span class="line"></span><br><span class="line">解决办法： <span class="number">1</span>、修改config\queuq.php默认队列名为low或high <span class="number">2</span>、启动队列进程时添加<span class="keyword">default</span>（--queue=high,<span class="keyword">default</span>,low）</span><br></pre></td></tr></table></figure>
<h3 id="合成图片"><a href="#合成图片" class="headerlink" title="合成图片"></a>合成图片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">require</span> <span class="string">'vendor/autoload.php'</span>;</span><br><span class="line"></span><br><span class="line">use Intervention\Image\ImageManager;</span><br><span class="line"></span><br><span class="line">$manager = <span class="keyword">new</span> ImageManager(array(<span class="string">'driver'</span> =&gt; <span class="string">'imagick'</span>));</span><br><span class="line">如果是在 Laravel 中，可直接通过 app(<span class="string">'image'</span>) 或使用 Intervention\Image\Facades\Image 门面类。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">use Intervention\Image\Facades\Image;</span><br><span class="line"></span><br><span class="line"><span class="comment">//或</span></span><br><span class="line">$manager = app(<span class="string">'image'</span>);</span><br><span class="line">合成</span><br><span class="line"></span><br><span class="line"><span class="comment">// $image = $manager-&gt;make($bg);</span></span><br><span class="line"><span class="comment">// $image = Image::make($bg);</span></span><br><span class="line"></span><br><span class="line">$image = app(<span class="string">'image'</span>)-&gt;make($bg);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 二维码图片https://godruoyi.com/posts/intervention-image-was-used-to-synthesize-images</span></span><br><span class="line">$qrcodeImage = app(<span class="string">'image'</span>)-&gt;make($qrcodeurl)-&gt;resize(<span class="number">200</span>, <span class="number">200</span>);</span><br><span class="line"><span class="comment">// 重置 头像 大小</span></span><br><span class="line">$avatarImage  = app(<span class="string">'image'</span>)-&gt;make($httpClient-&gt;request(<span class="string">'GET'</span>, $avatarurl)-&gt;getBody())-&gt;resize(<span class="number">200</span>, <span class="number">200</span>);</span><br><span class="line"></span><br><span class="line">$image-&gt;text(<span class="string">'Nickname'</span>, <span class="number">376</span>, <span class="number">320</span>, <span class="function"><span class="keyword">function</span> (<span class="params">$font</span>) </span>&#123;</span><br><span class="line">    $font-&gt;file(public_path(<span class="string">'fonts/SimHei.ttf'</span>));</span><br><span class="line">    $font-&gt;size(<span class="number">40</span>);</span><br><span class="line">    $font-&gt;color(<span class="string">'#000000'</span>);</span><br><span class="line">    $font-&gt;align(<span class="string">'center'</span>);</span><br><span class="line">    $font-&gt;valign(<span class="string">'top'</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">$image-&gt;insert($qrcodeImage, <span class="string">'bottom'</span>, <span class="number">0</span>, <span class="number">360</span>);</span><br><span class="line">$image-&gt;insert($avatarImage, <span class="string">'top'</span>, <span class="number">0</span>, <span class="number">105</span>);</span><br></pre></td></tr></table></figure>
<h3 id="http-code-204"><a href="#http-code-204" class="headerlink" title="http code 204"></a>http code 204</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">204</span> 为成功状态响应码，表示目前请求成功，但客户端不需要更新其现有页面。<span class="number">204</span> 响应默认是可以被缓存的。在响应中需要包含头信息 ETag。</span><br><span class="line"></span><br><span class="line">使用惯例是，在 PUT 请求中进行资源更新，但是不需要改变当前展示给用户的页面，那么返回 <span class="number">204</span> No Content。如果新创建了资源，那么返回 <span class="number">201</span> Created 。如果页面需要更新以反映更新后的资源，那么需要返回 <span class="number">200</span> 。</span><br><span class="line"></span><br><span class="line">所以大家在做 RestFul API 接口是，store、update 什么的，别全 <span class="number">200</span> <span class="number">200</span> 的返回了，不正规。https:<span class="comment">//learnku.com/articles/28056</span></span><br></pre></td></tr></table></figure>
<h3 id="json-encode-中文"><a href="#json-encode-中文" class="headerlink" title="json_encode 中文"></a>json_encode 中文</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public/index.php</span><br><span class="line"></span><br><span class="line">$response = $kernel-&gt;handle(</span><br><span class="line">    $request = Illuminate\Http\Request::capture()</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 取到内容https://learnku.com/articles/28054#topnav</span></span><br><span class="line">$content = $response-&gt;original;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 检查原始内容的类型是否需要转 json</span></span><br><span class="line"><span class="keyword">if</span> ($content <span class="keyword">instanceof</span> Arrayable ||</span><br><span class="line">    $content <span class="keyword">instanceof</span> Jsonable ||</span><br><span class="line">    $content <span class="keyword">instanceof</span> ArrayObject ||</span><br><span class="line">    $content <span class="keyword">instanceof</span> JsonSerializable ||</span><br><span class="line">    is_array($content)) &#123;</span><br><span class="line">    <span class="comment">// 重新设置响应内容</span></span><br><span class="line">    $response-&gt;setContent(json_encode($content, JSON_UNESCAPED_UNICODE));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">$response-&gt;send();</span><br></pre></td></tr></table></figure>
<h3 id="升级至Laravel-5-7报错setTrustedProxies-must-be-of-the-type-integer"><a href="#升级至Laravel-5-7报错setTrustedProxies-must-be-of-the-type-integer" class="headerlink" title="升级至Laravel 5.7报错setTrustedProxies() must be of the type integer"></a>升级至Laravel 5.7报错setTrustedProxies() must be of the type integer</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">打开 App\Http\Middleware\TrustProxies</span><br><span class="line"> </span><br><span class="line">将代码：http:<span class="comment">//www.ithov.net/php/1644.html</span></span><br><span class="line"> </span><br><span class="line">protected $headers = [</span><br><span class="line">        Request::<span class="function"><span class="params">HEADER_FORWARDED</span> =&gt;</span> <span class="string">'FORWARDED'</span>,</span><br><span class="line">        Request::<span class="function"><span class="params">HEADER_X_FORWARDED_FOR</span> =&gt;</span> <span class="string">'X_FORWARDED_FOR'</span>,</span><br><span class="line">        Request::<span class="function"><span class="params">HEADER_X_FORWARDED_HOST</span> =&gt;</span> <span class="string">'X_FORWARDED_HOST'</span>,</span><br><span class="line">        Request::<span class="function"><span class="params">HEADER_X_FORWARDED_PORT</span> =&gt;</span> <span class="string">'X_FORWARDED_PORT'</span>,</span><br><span class="line">        Request::<span class="function"><span class="params">HEADER_X_FORWARDED_PROTO</span> =&gt;</span> <span class="string">'X_FORWARDED_PROTO'</span>,</span><br><span class="line">    ];</span><br><span class="line"> </span><br><span class="line">修改为：</span><br><span class="line"> </span><br><span class="line">protected $headers = Request::HEADER_X_FORWARDED_ALL;</span><br></pre></td></tr></table></figure>
<h3 id="openssl-encrypt-expects-parameter-1-to-be-string"><a href="#openssl-encrypt-expects-parameter-1-to-be-string" class="headerlink" title="openssl_encrypt() expects parameter 1 to be string"></a>openssl_encrypt() expects parameter 1 to be string</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">5.5</span> 升级到 <span class="number">5.5</span><span class="number">.42</span> 后遇到的 Cookie 序列化问题</span><br><span class="line">Laravel 新版为了防止 PHP 对象的序列化/反序列化漏洞被利用，不再对 Cookie 值进行自动的序列化和反序列化处理。</span><br><span class="line">\Cookie::queue(<span class="string">'user'</span>, [<span class="string">'id'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span> =&gt; <span class="string">'admin'</span>], <span class="number">720</span>, <span class="string">'/'</span>)</span><br><span class="line"></span><br><span class="line">Laravel 更新到 v5<span class="number">.5</span><span class="number">.42</span> 后，因为 Laravel 不再自动对 Cookie 值 [<span class="string">'id'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span> =&gt; <span class="string">'admin'</span>] 进行序列化处理，而 openssl_encrypt ( string $data ... ) 只能加密字符串数据，这个时候程序就会抛出错误：openssl_encrypt() expects parameter <span class="number">1</span> to be string, array given。</span><br><span class="line"></span><br><span class="line">解决方法：https:<span class="comment">//segmentfault.com/a/1190000018799611</span></span><br><span class="line"></span><br><span class="line">新版里面在中间件 AppHttpMiddlewareEncryptCookies 新增静态属性 $serialize，当设置为 <span class="literal">true</span> 时可开启 Cookie 值的自动序列化和反序列化处理。</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Indicates if cookies should be serialized.</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @var bool</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">protected <span class="keyword">static</span> $serialize = <span class="literal">true</span>;</span><br><span class="line">【推荐】将 Cookie 值使用 <span class="built_in">JSON</span> 函数编码成字符串后再进行存储（获取 Cookie 值后需调用 <span class="built_in">JSON</span> 函数进行解码）。</span><br><span class="line">\Cookie::queue(<span class="string">'user'</span>, json_encode([<span class="string">'id'</span> =&gt; <span class="number">1</span>, <span class="string">'name'</span> =&gt; <span class="string">'admin'</span>]), <span class="number">720</span>, <span class="string">'/'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="多库下的DB-transaction-事务失效"><a href="#多库下的DB-transaction-事务失效" class="headerlink" title="多库下的DB::transaction()事务失效"></a>多库下的DB::transaction()事务失效</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">DB::transaction(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) <span class="title">use</span> (<span class="params">$uid, $roleId</span>) </span>&#123;</span><br><span class="line">   RoomUserRole::insert([</span><br><span class="line">     <span class="string">'uid'</span> =&gt; $uid,</span><br><span class="line">     <span class="string">'role_id'</span> =&gt; $roleId,</span><br><span class="line">     <span class="string">'created_at'</span> =&gt; LARAVEL_START,</span><br><span class="line">     <span class="string">'updated_at'</span> =&gt; LARAVEL_START</span><br><span class="line">   ]);</span><br><span class="line"></span><br><span class="line">   RoomUserRoleLog::insert([</span><br><span class="line">     <span class="string">'uid'</span> =&gt; $uid,</span><br><span class="line">     <span class="string">'handle_type'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">     <span class="string">'admin_uid'</span> =&gt; Auth::user()-&gt;id,</span><br><span class="line">     <span class="string">'created_at'</span> =&gt; LARAVEL_START,</span><br><span class="line">     <span class="string">'updated_at'</span> =&gt; LARAVEL_START</span><br><span class="line">   ]);</span><br><span class="line"></span><br><span class="line">&#125;);</span><br><span class="line">mysql 第二句会报错抛出一个异常， 查看数据库时第一句依然出入成功</span><br><span class="line">原文：https:<span class="comment">//blog.csdn.net/mathphp/article/details/82593779 </span></span><br><span class="line">DB::transaction()使用的是默认库的事务操作。所以要指定哪个数据库的事务</span><br><span class="line"></span><br><span class="line">DB::connection(<span class="string">'mysql2'</span>)-&gt;transaction(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) <span class="title">use</span> (<span class="params">$uid, $roleId</span>) </span>&#123;</span><br><span class="line">   RoomUserRole::insert([</span><br><span class="line">     <span class="string">'uid'</span> =&gt; $uid,</span><br><span class="line">     <span class="string">'role_id'</span> =&gt; $roleId,</span><br><span class="line">     <span class="string">'created_at'</span> =&gt; LARAVEL_START,</span><br><span class="line">     <span class="string">'updated_at'</span> =&gt; LARAVEL_START</span><br><span class="line">   ]);</span><br><span class="line"></span><br><span class="line">   RoomUserRoleLog::insert([</span><br><span class="line">     <span class="string">'uid'</span> =&gt; $uid,</span><br><span class="line">     <span class="string">'handle_type'</span> =&gt; <span class="number">1</span>,</span><br><span class="line">     <span class="string">'admin_uid'</span> =&gt; Auth::user()-&gt;id,</span><br><span class="line">     <span class="string">'created_at'</span> =&gt; LARAVEL_START,</span><br><span class="line">     <span class="string">'updated_at'</span> =&gt; LARAVEL_START</span><br><span class="line">   ]);</span><br><span class="line"></span><br><span class="line">&#125;); <span class="comment">// 这样你会发现事务才正常回滚</span></span><br><span class="line">DB::connection(<span class="string">'mysql_chat_room'</span>)-&gt;beginTransaction();</span><br><span class="line">DB::connection(<span class="string">'mysql_chat_room'</span>)-&gt;commit();</span><br><span class="line">DB::connection(<span class="string">'mysql_chat_room'</span>)-&gt;rollBack(); <span class="comment">// 指定库，不然都会跑默认配置库的事务</span></span><br></pre></td></tr></table></figure>
<h3 id="执行时间和内存消耗"><a href="#执行时间和内存消耗" class="headerlink" title="执行时间和内存消耗"></a>执行时间和内存消耗</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getElapsedTime</span>(<span class="params">int $decimals  = <span class="number">2</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> number_format(microtime(<span class="literal">true</span>) - request()-&gt;server(<span class="string">'REQUEST_TIME_FLOAT'</span>), $decimals) . <span class="string">' s'</span>;</span><br><span class="line">&#125;</span><br><span class="line">内存使用：</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">getMemoryUsage</span>(<span class="params">precision = <span class="number">2</span></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $size = memory_get_usage(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        $unit = [<span class="string">'b'</span>, <span class="string">'kb'</span>, <span class="string">'mb'</span>, <span class="string">'gb'</span>, <span class="string">'tb'</span>, <span class="string">'pb'</span>];</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> round($size / pow(<span class="number">1024</span>, ($i = floor(log($size, <span class="number">1024</span>)))), $precision) . <span class="string">' '</span> . $unit[$i];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="速查表"><a href="#速查表" class="headerlink" title="速查表"></a>速查表</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 针对命令显示帮助信息</span></span><br><span class="line">php artisan --help OR -h</span><br><span class="line"><span class="comment">// 抑制输出信息</span></span><br><span class="line">php artisan --quiet OR -q</span><br><span class="line"><span class="comment">// 打印 Laravel 的版本信息</span></span><br><span class="line">php artisan --version OR -V</span><br><span class="line"><span class="comment">// 不询问任何交互性的问题</span></span><br><span class="line">php artisan --no-interaction OR -n</span><br><span class="line"><span class="comment">// 强制输出 ANSI 格式</span></span><br><span class="line">php artisan --ansi</span><br><span class="line"><span class="comment">// 禁止输出 ANSI 格式</span></span><br><span class="line">php artisan --no-ansi</span><br><span class="line"><span class="comment">// 显示当前命令行运行的环境</span></span><br><span class="line">php artisan --env</span><br><span class="line"><span class="comment">// -v|vv|vvv 通过增加 v 的个数来控制命令行输出内容的详尽情况: 1 个代表正常输出, 2 个代表输出更多消息, 3 个代表调试</span></span><br><span class="line">php artisan --verbose</span><br><span class="line"><span class="comment">// 移除编译优化过的文件 (storage/frameworks/compiled.php)</span></span><br><span class="line">php artisan clear-compiled</span><br><span class="line"><span class="comment">// 显示当前框架运行的环境</span></span><br><span class="line">php artisan env</span><br><span class="line"><span class="comment">// 显示某个命令的帮助信息</span></span><br><span class="line">php artisan help</span><br><span class="line"><span class="comment">// 显示所有可用的命令</span></span><br><span class="line">php artisan list</span><br><span class="line"><span class="comment">// 进入应用交互模式</span></span><br><span class="line">php artisan tinker</span><br><span class="line"><span class="comment">// 配合 dump() 函数调试数据</span></span><br><span class="line">php artisan dump-server</span><br><span class="line"><span class="comment">// 进入维护模式</span></span><br><span class="line">php artisan down</span><br><span class="line"><span class="comment">// 退出维护模式</span></span><br><span class="line">php artisan up</span><br><span class="line"><span class="comment">// 优化框架性能</span></span><br><span class="line"><span class="comment">// --force    强制编译已写入文件 (storage/frameworks/compiled.php)</span></span><br><span class="line"><span class="comment">// --psr      不对 Composer 的 dump-autoload 进行优化</span></span><br><span class="line">php artisan optimize [--force] [--psr]</span><br><span class="line"><span class="comment">// 更改前端预设</span></span><br><span class="line"><span class="comment">// type_name (可以是 none, bootstrap, vue, react)</span></span><br><span class="line">php artisan preset [options] [--] type_name</span><br><span class="line"><span class="comment">// 启动内置服务器</span></span><br><span class="line">php artisan serve</span><br><span class="line"><span class="comment">// 更改默认端口</span></span><br><span class="line">php artisan serve --port <span class="number">8080</span></span><br><span class="line"><span class="comment">// 使其在本地服务器外也可正常工作</span></span><br><span class="line">php artisan serve --host <span class="number">0.0</span><span class="number">.0</span><span class="number">.0</span></span><br><span class="line"><span class="comment">// 更改应用命名空间</span></span><br><span class="line">php artisan app:name namespace</span><br><span class="line"><span class="comment">// 清除过期的密码重置令牌</span></span><br><span class="line">php artisan auth:clear-resets</span><br><span class="line"></span><br><span class="line"><span class="comment">// 清空应用缓存</span></span><br><span class="line">php artisan cache:clear</span><br><span class="line"><span class="comment">// 移除 key_name 对应的缓存</span></span><br><span class="line">php artisan cache:forget key_name [&lt;store&gt;]</span><br><span class="line">// 创建缓存数据库表 migration</span><br><span class="line">php artisan cache:table</span><br><span class="line"></span><br><span class="line">// 合并所有的配置信息为一个，提高加载速度</span><br><span class="line">php artisan config:cache</span><br><span class="line">// 移除配置缓存文件</span><br><span class="line">php artisan config:clear</span><br><span class="line"></span><br><span class="line">// 程序内部调用 Artisan 命令</span><br><span class="line">$exitCode = Artisan::call('config:cache');</span><br><span class="line">// 运行所有的 seed 假数据生成类</span><br><span class="line">// --class      可以指定运行的类，默认是: "DatabaseSeeder"</span><br><span class="line">// --database   可以指定数据库</span><br><span class="line">// --force      当处于生产环境时强制执行操作</span><br><span class="line">php artisan db:seed [--class[="..."]] [--database[="..."]] [--force]</span><br><span class="line"></span><br><span class="line">// 基于注册的信息，生成遗漏的 events 和 handlers</span><br><span class="line">php artisan event:generate</span><br><span class="line">// 罗列所有事件和监听器</span><br><span class="line">php artisan event:list</span><br><span class="line">// 缓存事件和监听器</span><br><span class="line">php artisan event:cache</span><br><span class="line">// 清除事件和监听器缓存</span><br><span class="line">php artisan event:clear</span><br><span class="line"></span><br><span class="line">// 生成新的处理器类</span><br><span class="line">// --command      需要处理器处理的命令类名字</span><br><span class="line">php artisan handler:command [--command="..."] name</span><br><span class="line">// 创建一个新的时间处理器类</span><br><span class="line">// --event        需要处理器处理的事件类名字</span><br><span class="line">// --queued       需要处理器使用队列话处理的事件类名字</span><br><span class="line">php artisan handler:event [--event="..."] [--queued] name</span><br><span class="line"></span><br><span class="line">// 生成应用的 key（会覆盖）</span><br><span class="line">php artisan key:generate</span><br><span class="line"></span><br><span class="line">// 发布本地化翻译文件到 resources 文件下</span><br><span class="line">// locales: 逗号分隔，如 zh_CN,tk,th [默认是: "all"]</span><br><span class="line">php artisan lang:publish [options] [--] [&lt;locales&gt;]</span><br><span class="line"></span><br><span class="line">// 创建用户认证脚手架</span><br><span class="line">php artisan make:auth</span><br><span class="line">// 创建 Channel 类</span><br><span class="line">php artisan make:channel name</span><br><span class="line">// 在默认情况下, 这将创建未加入队列的自处理命令</span><br><span class="line">// 通过 --handler 标识来生成一个处理器, 用 --queued 来使其入队列.</span><br><span class="line">php artisan make:command [--handler] [--queued] name</span><br><span class="line">// 创建一个新的 Artisan 命令</span><br><span class="line">//  --command     命令被调用的名称。 (默认为: "command:name")</span><br><span class="line">php artisan make:console [--command[="..."]] name</span><br><span class="line">// 创建一个新的资源控制器</span><br><span class="line">// --plain      生成一个空白的控制器类</span><br><span class="line">php artisan make:controller [--plain] name</span><br><span class="line">php artisan make:controller App\\Admin\\Http\\Controllers\\DashboardController</span><br><span class="line">// 创建一个新的事件类</span><br><span class="line">php artisan make:event name</span><br><span class="line">// 创建异常类</span><br><span class="line">php artisan make:exception name</span><br><span class="line">// 创建模型工厂类</span><br><span class="line">php artisan make:factory name</span><br><span class="line">// 创建一个队列任务文件</span><br><span class="line">php artisan make:job </span><br><span class="line">// 创建一个监听者类</span><br><span class="line">php artisan make:listener name</span><br><span class="line">// 创建一个新的邮件类</span><br><span class="line">php artisan make:mail name</span><br><span class="line">// 创建一个新的中间件类</span><br><span class="line">php artisan make:middleware name</span><br><span class="line">// 创建一个新的迁移文件</span><br><span class="line">// --create     将被创建的数据表.</span><br><span class="line">// --table      将被迁移的数据表.</span><br><span class="line">php artisan make:migration [--create[="..."]] [--table[="..."]] name</span><br><span class="line">// 创建一个新的 Eloquent 模型类</span><br><span class="line">php artisan make:model User</span><br><span class="line">php artisan make:model Models/User</span><br><span class="line">// 新建一个消息通知类</span><br><span class="line">php artisan make:notification TopicRepliedNotification</span><br><span class="line">// 新建一个模型观察者类</span><br><span class="line">php artisan make:observer UserObserver</span><br><span class="line">// 创建授权策略</span><br><span class="line">php artisan make:policy PostPolicy</span><br><span class="line">// 创建一个新的服务提供者类</span><br><span class="line">php artisan make:provider name</span><br><span class="line">// 创建一个新的表单请求类</span><br><span class="line">php artisan make:request name</span><br><span class="line">// 创建一个 API 资源类</span><br><span class="line">php artisan make:resource name</span><br><span class="line">// 新建验证规则类</span><br><span class="line">php artisan make:rule name</span><br><span class="line">// 创建模型脚手架</span><br><span class="line">// &lt;name&gt; 模型名称，如 Post</span><br><span class="line">// -s, --schema=SCHEMA 表结构如：--schema="title:string"</span><br><span class="line">// -a, --validator[=VALIDATOR] 表单验证，如：--validator="title:required"</span><br><span class="line">// -l, --localization[=LOCALIZATION] 设置本地化信息，如：--localization="key:value"</span><br><span class="line">// -b, --lang[=LANG] 设置本地化语言 --lang="en"</span><br><span class="line">// -f, --form[=FORM] 使用 Illumintate/Html Form 来生成表单选项，默认为 false</span><br><span class="line">// -p, --prefix[=PREFIX] 表结构前缀，默认 false</span><br><span class="line">php artisan make:scaffold  [options] [--] &lt;name&gt;</span><br><span class="line">// 生成数据填充类</span><br><span class="line">php artisan make:seeder</span><br><span class="line">// 生成测试类</span><br><span class="line">php artisan make:test</span><br><span class="line"></span><br><span class="line">// 数据库迁移</span><br><span class="line">// --database   指定数据库连接（下同）</span><br><span class="line">// --force      当处于生产环境时强制执行，不询问（下同）</span><br><span class="line">// --path       指定单独迁移文件地址</span><br><span class="line">// --pretend    把将要运行的 SQL 语句打印出来（下同）</span><br><span class="line">// --seed       Seed 任务是否需要被重新运行（下同）</span><br><span class="line">php artisan migrate [--database[="..."]] [--force] [--path[="..."]] [--pretend] [--seed]</span><br><span class="line">// 创建迁移数据库表</span><br><span class="line">php artisan migrate:install [--database[="..."]]</span><br><span class="line">// Drop 所有数据表并重新运行 Migration</span><br><span class="line">php artisan migrate:fresh</span><br><span class="line">// 重置并重新运行所有的 migrations</span><br><span class="line">// --seeder     指定主 Seeder 的类名</span><br><span class="line">php artisan migrate:refresh [--database[="..."]] [--force] [--seed] [--seeder[="..."]]</span><br><span class="line">// 回滚所有的数据库迁移</span><br><span class="line">php artisan migrate:reset [--database[="..."]] [--force] [--pretend]</span><br><span class="line">// 回滚最最近一次运行的迁移任务</span><br><span class="line">php artisan migrate:rollback [--database[="..."]] [--force] [--pretend]</span><br><span class="line">// migrations 数据库表信息</span><br><span class="line">php artisan migrate:status</span><br><span class="line"></span><br><span class="line">// 为数据库消息通知创建一个表迁移类</span><br><span class="line">php artisan notifications:table</span><br><span class="line">// 清除缓存的 bootstrap 文件</span><br><span class="line">php artisan optimize:clear</span><br><span class="line">// 扩展包自动发现</span><br><span class="line">php artisan package:discover</span><br><span class="line"></span><br><span class="line">// 为队列数据库表创建一个新的迁移</span><br><span class="line">php artisan queue:table</span><br><span class="line">// 监听指定的队列</span><br><span class="line">// --queue      被监听的队列</span><br><span class="line">// --delay      给执行失败的任务设置延时时间 (默认为零: 0)</span><br><span class="line">// --memory     内存限制大小，单位为 MB (默认为: 128)</span><br><span class="line">// --timeout    指定任务运行超时秒数 (默认为: 60)</span><br><span class="line">// --sleep      等待检查队列任务的秒数 (默认为: 3)</span><br><span class="line">// --tries      任务记录失败重试次数 (默认为: 0)</span><br><span class="line">php artisan queue:listen [--queue[="..."]] [--delay[="..."]] [--memory[="..."]] [--timeout[="..."]] [--sleep[="..."]] [--tries[="..."]] [connection]</span><br><span class="line">// 查看所有执行失败的队列任务</span><br><span class="line">php artisan queue:failed</span><br><span class="line">// 为执行失败的数据表任务创建一个迁移</span><br><span class="line">php artisan queue:failed-table</span><br><span class="line">// 清除所有执行失败的队列任务</span><br><span class="line">php artisan queue:flush</span><br><span class="line">// 删除一个执行失败的队列任务</span><br><span class="line">php artisan queue:forget</span><br><span class="line">// 在当前的队列任务执行完毕后, 重启队列的守护进程</span><br><span class="line">php artisan queue:restart</span><br><span class="line">// 对指定 id 的执行失败的队列任务进行重试(id: 失败队列任务的 ID)</span><br><span class="line">php artisan queue:retry id</span><br><span class="line">// 指定订阅 Iron.io 队列的链接</span><br><span class="line">// queue: Iron.io 的队列名称.</span><br><span class="line">// url: 将被订阅的 URL.</span><br><span class="line">// --type       指定队列的推送类型.</span><br><span class="line">php artisan queue:subscribe [--type[="..."]] queue url</span><br><span class="line">// 处理下一个队列任务</span><br><span class="line">// --queue      被监听的队列</span><br><span class="line">// --daemon     在后台模式运行</span><br><span class="line">// --delay      给执行失败的任务设置延时时间 (默认为零: 0)</span><br><span class="line">// --force      强制在「维护模式下」运行</span><br><span class="line">// --memory     内存限制大小，单位为 MB (默认为: 128)</span><br><span class="line">// --sleep      当没有任务处于有效状态时, 设置其进入休眠的秒数 (默认为: 3)</span><br><span class="line">// --tries      任务记录失败重试次数 (默认为: 0)</span><br><span class="line">php artisan queue:work [--queue[="..."]] [--daemon] [--delay[="..."]] [--force] [--memory[="..."]] [--sleep[="..."]] [--tries[="..."]] [connection]</span><br><span class="line"></span><br><span class="line">// 生成路由缓存文件来提升路由效率</span><br><span class="line">php artisan route:cache</span><br><span class="line">// 移除路由缓存文件</span><br><span class="line">php artisan route:clear</span><br><span class="line">// 显示已注册过的路由</span><br><span class="line">php artisan route:list</span><br><span class="line"></span><br><span class="line">// 运行计划命令</span><br><span class="line">php artisan schedule:run</span><br><span class="line"></span><br><span class="line">// 为 session 数据表生成迁移文件</span><br><span class="line">php artisan session:table</span><br><span class="line">// 创建 "public/storage" 到 "storage/app/public" 的软链接</span><br><span class="line">php artisan storage:link</span><br><span class="line"></span><br><span class="line">// 从 vendor 的扩展包中发布任何可发布的资源</span><br><span class="line">// --force        重写所有已存在的文件</span><br><span class="line">// --provider     指定你想要发布资源文件的服务提供者</span><br><span class="line">// --tag          指定你想要发布标记资源.</span><br><span class="line">php artisan vendor:publish [--force] [--provider[="..."]] [--tag[="..."]]</span><br><span class="line">php artisan tail [--path[="..."]] [--lines[="..."]] [connection]</span><br><span class="line"></span><br><span class="line">// 缓存视图文件以提高效率</span><br><span class="line">php artisan view:cache</span><br><span class="line">// 清除视图文件缓存</span><br><span class="line">php artisan view:clear</span><br><span class="line"></span><br><span class="line">https://laravelcode.cn/docs/5.8</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-Excel"><a href="#Laravel-Excel" class="headerlink" title="Laravel Excel"></a>Laravel Excel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> maatwebsite/excel</span><br><span class="line">php artisan vendor:publish --provider=<span class="string">"Maatwebsite\Excel\ExcelServiceProvider"</span></span><br><span class="line"></span><br><span class="line">php artisan make:<span class="keyword">export</span> UsersExport --model=User</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace App\Exports;</span><br><span class="line"></span><br><span class="line">use App\User;</span><br><span class="line">use Maatwebsite\Excel\Concerns\FromCollection;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UsersExport</span> <span class="title">implements</span> <span class="title">FromCollection</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">collection</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> User::all();</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">控制器中调用</span><br><span class="line"></span><br><span class="line">use App\Exports\UsersExport;</span><br><span class="line">use Maatwebsite\Excel\Facades\Excel;</span><br><span class="line">use App\Http\Controllers\Controller;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UsersController</span> <span class="keyword">extends</span> <span class="title">Controller</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">export</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Excel::download(<span class="keyword">new</span> UsersExport, <span class="string">'users.xlsx'</span>);</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/29089</span></span><br></pre></td></tr></table></figure>
<h3 id="模型过滤"><a href="#模型过滤" class="headerlink" title="模型过滤"></a>模型过滤</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Book::query()</span><br><span class="line">        -&gt;where(<span class="string">'title'</span>, <span class="string">'like'</span>, <span class="string">'%我们%'</span>)</span><br><span class="line">        -&gt;where(<span class="string">'price'</span>, <span class="string">'&gt;='</span>, <span class="number">30</span>)</span><br><span class="line">        -&gt;where(<span class="string">'publisher'</span>, <span class="string">'大地出版社'</span>)</span><br><span class="line">        -&gt;orderBy(<span class="string">'id'</span>, <span class="string">'DESC'</span>)</span><br><span class="line">        -&gt;get();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">abstract <span class="class"><span class="keyword">class</span> <span class="title">QueryFilter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line"></span><br><span class="line">    protected $request;</span><br><span class="line">    protected $builder;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Request $request</span>)                                                                        </span></span><br><span class="line"><span class="function">    </span>&#123;                                                                                                                    </span><br><span class="line">        $<span class="keyword">this</span>-&gt;request = $request;                                                                                       </span><br><span class="line">    &#125;                                                                                                                    </span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">apply</span>(<span class="params">Builder $builder</span>)                                                                              </span></span><br><span class="line"><span class="function">    </span>&#123;                                                                                                                    </span><br><span class="line">        $<span class="keyword">this</span>-&gt;builder = $builder;                                                                                       </span><br><span class="line"></span><br><span class="line">        foreach ($<span class="keyword">this</span>-&gt;filters() <span class="keyword">as</span> $name =&gt; $value) &#123;                                                                  </span><br><span class="line">            <span class="keyword">if</span> (method_exists($<span class="keyword">this</span>, $name)) &#123;                                                                           </span><br><span class="line">                call_user_func_array([$<span class="keyword">this</span>, $name], array_filter([$value]));                                            </span><br><span class="line">        &#125;                                                                                                            </span><br><span class="line">     &#125;                                                                                                                </span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder;                                                                                           </span><br><span class="line">    &#125;                                                                                                                    </span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">filters</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;request-&gt;all();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BookFilter</span> <span class="keyword">extends</span> <span class="title">QueryFilter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">title</span>(<span class="params">$title</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder-&gt;where(<span class="string">'title'</span>, <span class="string">'like'</span>, <span class="string">"%&#123;$title&#125;%"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">price</span>(<span class="params">$price</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder-&gt;where(<span class="string">'price'</span>, <span class="string">'&gt;='</span>, <span class="string">"%&#123;$price&#125;%"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">publisher</span>(<span class="params">$publisher</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;builder-&gt;where(<span class="string">'publisher'</span>, $publisher);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">通过 URL 的参数来进行动态查询。</span><br><span class="line"></span><br><span class="line">books?title=我们    <span class="comment">//  查找标题含有我们的图书</span></span><br><span class="line"></span><br><span class="line">books?title=我们&amp;price=<span class="number">25</span>   <span class="comment">//  查找标题含有我们并且价格大于25元的图书</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Book</span> <span class="keyword">extends</span> <span class="title">Model</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">scopeFilter</span>(<span class="params">$query, QueryFilter $filters</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $filters-&gt;apple($query);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">最后，我们原来控制器的方法将改为下面的样子：</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">index</span>(<span class="params">BookFilter $filters</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> Book::filter($filters)-&gt;get();</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/29100#reply91762</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-配置双模板"><a href="#Laravel-配置双模板" class="headerlink" title="Laravel 配置双模板"></a>Laravel 配置双模板</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> whichbrowser/parser</span><br><span class="line">php artisan make:middleware Template</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Template</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $except = [];</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">$request, Closure $next</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $result = <span class="keyword">new</span> WhichBrowser\Parser(getallheaders());</span><br><span class="line">        <span class="comment">// 如果是桌面类型, 返回true</span></span><br><span class="line">        $isDesktop = $result-&gt;isType(<span class="string">'desktop'</span>);</span><br><span class="line">        <span class="keyword">if</span> ($isDesktop) &#123;</span><br><span class="line">            <span class="comment">// 加载pc端的模板文件</span></span><br><span class="line">            $path = resource_path(<span class="string">'views/pc/'</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="comment">// 加载mobile端的模板文件</span></span><br><span class="line">            $path = resource_path(<span class="string">'views/mobile/'</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 获取视图查找器实例</span></span><br><span class="line">        $view = app(<span class="string">'view'</span>)-&gt;getFinder();</span><br><span class="line">        <span class="comment">// 重新定义视图目录</span></span><br><span class="line">        $view-&gt;prependLocation($path);</span><br><span class="line">        <span class="comment">// 返回请求</span></span><br><span class="line">        <span class="keyword">return</span> $next($request);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">protected $middleware = [</span><br><span class="line">    \App\Http\Middleware\Template::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">];</span><br><span class="line"><span class="keyword">return</span> view(<span class="string">'registration.index'</span>, $data);</span><br><span class="line">如从 PC 设备打开网页：加载 /resources/views/pc/registration/index.blade.php 模板</span><br><span class="line"></span><br><span class="line">如从移动设备打开网页：加载 /resources/views/mobile/registration/index.blade.php 模板</span><br><span class="line">https:<span class="comment">//learnku.com/articles/24789</span></span><br></pre></td></tr></table></figure>
<h3 id="助手函数"><a href="#助手函数" class="headerlink" title="助手函数"></a>助手函数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">php artisan make:provider HelperServiceProvider</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    foreach(glob(app_path(<span class="string">'Helpers'</span>) . <span class="string">'/*.php'</span>) <span class="keyword">as</span> $file) &#123;</span><br><span class="line">        require_once $file;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">App\Providers\HelperServiceProvider::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line"><span class="class"><span class="title">function</span> <span class="title">carbon</span>($<span class="title">time</span> </span>= <span class="literal">null</span>, $tz = <span class="literal">null</span>)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> \Carbon\Carbon($time, $tz);</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//blog.foof.dev/posts/laravel-elegant-add-helper-function-0</span></span><br></pre></td></tr></table></figure>
<h3 id="创建无限极分类树"><a href="#创建无限极分类树" class="headerlink" title="创建无限极分类树"></a>创建无限极分类树</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getList</span>(<span class="params">$catList</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $treeData = [];</span><br><span class="line">    foreach ($catList <span class="keyword">as</span> &amp;$item) &#123;</span><br><span class="line">        $parent_id = $item[<span class="string">'parent_id'</span>];</span><br><span class="line">        <span class="keyword">if</span> (isset($catList[$parent_id]) &amp;&amp; !empty($catList[$parent_id])) &#123;</span><br><span class="line">            $catList[$parent_id][<span class="string">'list'</span>][] = &amp;$catList[$item[<span class="string">'id'</span>]];</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            $treeData[] = &amp;$catList[$item[<span class="string">'id'</span>]];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    unset($item);</span><br><span class="line">    <span class="keyword">return</span> $treeData[<span class="number">0</span>][<span class="string">'list'</span>]; <span class="comment">// 根节点只有中华人民共和国，所以直接返回中国的所有子节点</span></span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/30088</span></span><br></pre></td></tr></table></figure>
<h3 id="任务重复执行"><a href="#任务重复执行" class="headerlink" title="任务重复执行"></a>任务重复执行</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">当前任务还没有执行完毕的时候另一个相同的任务也会执行，从而导致任务重复。例如想象一下我们执行每分钟生成一次报告的任务，在经过一段时间后，数据量变得很大导致执行时间多于 <span class="number">1</span> 分钟，这样就会导致在上一个任务还没结束的时候另一个相同的任务开始执行</span><br><span class="line"></span><br><span class="line">当我们在开会进行激烈的讨论时，我会从我桌子里拿出来一个尖叫鸡。只有手里拿着尖叫鸡的人才能说话，如果你没有拿着尖叫鸡你是不能说话的。你只能向会议主持人请示，只有在你拿到尖叫鸡的时候你才能说话否则只能等待。当你讲话完毕的时候，将尖叫鸡还给会议主持人，主持人会将尖叫鸡给到下一个人来让其说话。这样会确保人们不会互相交谈，同时他们也会有自己的时间来进行讲话。</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">create</span>(<span class="params">Event $event</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;cache-&gt;add($event-&gt;mutexName(), <span class="literal">true</span>, <span class="number">1440</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">exists</span>(<span class="params">Event $event</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;cache-&gt;has($event-&gt;mutexName());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">forget</span>(<span class="params">Event $event</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $<span class="keyword">this</span>-&gt;cache-&gt;forget($event-&gt;mutexName());</span><br><span class="line">&#125;</span><br><span class="line">register_shutdown_function(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    $<span class="keyword">this</span>-&gt;removeMutex();</span><br><span class="line">&#125;);</span><br><span class="line">https:<span class="comment">//learnku.com/articles/30550</span></span><br></pre></td></tr></table></figure>
<h3 id="创建一个命令"><a href="#创建一个命令" class="headerlink" title="创建一个命令"></a>创建一个命令</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">$ php artisan make:command HashCommand</span><br><span class="line">$signature 属性，设置命令名、命令参数、命令选项；</span><br><span class="line">$description 属性，设置命令的描述信息；</span><br><span class="line">handle() 方法，包含实际的命令逻辑代码。</span><br><span class="line">修改 $signature 属性，设置 hash 命令的命令名、命令参数、命令选项：</span><br><span class="line"></span><br><span class="line">protected $signature = <span class="string">'hash &#123;text&#125; &#123;--U|uppercase&#125;'</span>;</span><br><span class="line">  public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $text = $<span class="keyword">this</span>-&gt;argument(<span class="string">'text'</span>);</span><br><span class="line">        $uppercase = $<span class="keyword">this</span>-&gt;option(<span class="string">'uppercase'</span>);</span><br><span class="line"></span><br><span class="line">        $md5text = $uppercase ? strtoupper(md5($text)) : md5($text);</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;info(<span class="string">"md5('&#123;$text&#125;') = $md5text"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">$ php artisan hash <span class="string">"hello world"</span></span><br><span class="line">md5(<span class="string">'hello world'</span>) = <span class="number">5</span>eb63bbbe01eeed093cb22bb8f5acdc3</span><br><span class="line">带上 -U 选项：</span><br><span class="line"></span><br><span class="line">$ php artisan hash <span class="string">"hello world"</span> -U</span><br><span class="line">md5(<span class="string">'hello world'</span>) = <span class="number">5</span>EB63BBBE01EEED093CB22BB8F5ACDC3</span><br></pre></td></tr></table></figure>
<h3 id="自定义-Laravel-的-404-页面"><a href="#自定义-Laravel-的-404-页面" class="headerlink" title="自定义 Laravel 的 404 页面"></a>自定义 Laravel 的 404 页面</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">app/Exceptions/Handler.php 文件中修改 render 方法</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">$request, Exception $exception</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;isHttpException($exception)) &#123;</span><br><span class="line">        <span class="keyword">if</span> ($exception-&gt;getStatusCode() == <span class="number">404</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> response()-&gt;view(<span class="string">'errors.'</span> . <span class="string">'404'</span>, [], <span class="number">404</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> parent::render($request, $exception);</span><br><span class="line">&#125;创建一个新的视图 errors/<span class="number">404.</span>blade.php 。</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">$request, Exception $exception</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;isHttpException($exception)) &#123;</span><br><span class="line">        <span class="keyword">if</span> (view()-&gt;exists(<span class="string">'errors.'</span> . $exception-&gt;getStatusCode())) &#123;</span><br><span class="line">            <span class="keyword">return</span> response()-&gt;view(<span class="string">'errors.'</span> . $exception-&gt;getStatusCode(), [], $exception-&gt;getStatusCode());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> parent::render($request, $exception);</span><br><span class="line">&#125;</span><br><span class="line">创建一个 自定义异常 。运行以下代码来创建一个名为 TestingHttpException 的异常。</span><br><span class="line"></span><br><span class="line">php artisan make:exception TestingHttpException</span><br><span class="line">在  app/Exceptions/Handler.php  文件中，修改 render 方法。</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">render</span>(<span class="params">$request, Exception $exception</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> ($exception <span class="keyword">instanceof</span> TestingHttpException) &#123;</span><br><span class="line">        <span class="keyword">return</span> response()-&gt;view(<span class="string">'errors.testing'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> parent::render($request, $exception);</span><br><span class="line">&#125;</span><br><span class="line">如果异常是 TestingHttpException 的实例，它将返回 errors.testing 视图。https:<span class="comment">//learnku.com/laravel/t/27873</span></span><br></pre></td></tr></table></figure>
<h3 id="图片处理"><a href="#图片处理" class="headerlink" title="图片处理"></a>图片处理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> intervention/image  https:<span class="comment">//learnku.com/laravel/t/30978</span></span><br><span class="line"> <span class="comment">// 创建一张空的画布，像素3628x1757，背景白色</span></span><br><span class="line">   $img = Image::canvas(<span class="number">3628</span>, <span class="number">1757</span>, <span class="string">'#fff'</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 获取本地图片，可以获取input上传文件</span></span><br><span class="line">     $leftImage = Image::make(base_path().<span class="string">'/public/p.jpg'</span>)-&gt;resize(<span class="number">2255</span>, <span class="number">1305</span>);</span><br><span class="line">     $rightImage = Image::make(base_path().<span class="string">'/public/f.jpg'</span>)-&gt;resize(<span class="number">1000</span>, <span class="number">1000</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 插入到画布，left-top是距离左侧和顶部，值对应的是后面 100 100 处</span></span><br><span class="line">     $img-&gt;insert($leftImage, <span class="string">'left-top'</span>, <span class="number">100</span>, <span class="number">100</span>);</span><br><span class="line">     $img-&gt;insert($rightImage, <span class="string">'right-top'</span>, <span class="number">100</span>, <span class="number">250</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 插入文本，通过回调设置参数，file上传的是字体库，需要自己下载放入</span></span><br><span class="line">     $img-&gt;text(<span class="string">'王*军       320830**********542'</span>, <span class="number">1757</span>, <span class="number">1550</span>, <span class="function"><span class="keyword">function</span>(<span class="params">$font</span>) </span>&#123;</span><br><span class="line">        $font-&gt;file(base_path().<span class="string">'/public/fzjw.ttf'</span>);</span><br><span class="line">        $font-&gt;size(<span class="number">160</span>);</span><br><span class="line">        $font-&gt;color(<span class="string">'#000'</span>);</span><br><span class="line">        $font-&gt;align(<span class="string">'center'</span>);</span><br><span class="line">        $font-&gt;valign(<span class="string">'top'</span>);</span><br><span class="line">        $font-&gt;angle(<span class="number">0</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">//可以直接返回图像，也可通过$img-&gt;save()进行保存图片</span></span><br><span class="line">    <span class="keyword">return</span> $img-&gt;response(<span class="string">'jpg'</span>);</span><br></pre></td></tr></table></figure>
<h3 id="取消事件触发"><a href="#取消事件触发" class="headerlink" title="取消事件触发"></a>取消事件触发</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取model里面的事件</span></span><br><span class="line">$dispatcher = YourModel::getEventDispatcher();</span><br><span class="line"></span><br><span class="line"><span class="comment">//禁用model里面的事件</span></span><br><span class="line">YourModel::unsetEventDispatcher();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 调用model的save方法</span></span><br><span class="line">$yourInstance-&gt;save();</span><br><span class="line"></span><br><span class="line"><span class="comment">//启用事件</span></span><br><span class="line">YourModel::setEventDispatcher($dispatcher);</span><br><span class="line"><span class="comment">//Visitor model有saved事件，某些地方某些情况监听到后做一些其他操作</span></span><br><span class="line">protected $dispatchesEvents = [</span><br><span class="line">        <span class="string">'saved'</span> =&gt; VisitorSavedEvent::<span class="class"><span class="keyword">class</span>,</span></span><br><span class="line">    ];</span><br><span class="line"><span class="comment">//有时候不需要触发事件，是可以取消的，可以调用model的update方法避开save()或者取消事件（推荐后者）。</span></span><br><span class="line"><span class="comment">// 获取来访信息</span></span><br><span class="line">        $visitor = Visitor::where(<span class="string">'id'</span>, $request-&gt;visitor_id)-&gt;first();</span><br><span class="line"><span class="comment">//取消事件触发https://learnku.com/articles/31490</span></span><br><span class="line">$dispatcher = Visitor::getEventDispatcher();</span><br><span class="line">Visitor::unsetEventDispatcher();</span><br><span class="line">$ret = $visitor-&gt;save();</span><br><span class="line">Visitor::setEventDispatcher($dispatcher);</span><br><span class="line"><span class="comment">//5.7</span></span><br><span class="line">YourModel::withoutEvents(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">     <span class="comment">// do something...</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h3 id="记录memcache-mongodb运行时间"><a href="#记录memcache-mongodb运行时间" class="headerlink" title="记录memcache mongodb运行时间"></a>记录memcache mongodb运行时间</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br></pre></td><td class="code"><pre><span class="line">cache   \vendor\laravel\framework\src\Illuminate\Cache\Repository.php</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">Store $store</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;start = microtime(<span class="literal">true</span>);</span><br><span class="line">		$<span class="keyword">this</span>-&gt;store = $store;</span><br><span class="line">	&#125;</span><br><span class="line">protected <span class="function"><span class="keyword">function</span> <span class="title">fireCacheEvent</span>(<span class="params">$event, $payload</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">        <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * 添加cache响应时间</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        $total = round((microtime(<span class="literal">true</span>) - $<span class="keyword">this</span>-&gt;start) * <span class="number">1000</span>, <span class="number">2</span>);</span><br><span class="line">        $payload[] = $total;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> (isset($<span class="keyword">this</span>-&gt;events))</span><br><span class="line">		&#123;</span><br><span class="line">			$<span class="keyword">this</span>-&gt;events-&gt;fire(<span class="string">'cache.'</span>.$event, $payload);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	public <span class="function"><span class="keyword">function</span> <span class="title">put</span>(<span class="params">$key, $value, $minutes</span>)</span></span><br><span class="line"><span class="function">    	</span>&#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;start = microtime(<span class="literal">true</span>);</span><br><span class="line">    </span><br><span class="line">    		$minutes = $<span class="keyword">this</span>-&gt;getMinutes($minutes);</span><br><span class="line">    </span><br><span class="line">    		<span class="keyword">if</span> ( ! is_null($minutes))</span><br><span class="line">    		&#123;</span><br><span class="line">    			$<span class="keyword">this</span>-&gt;store-&gt;put($key, $value, $minutes);</span><br><span class="line">    </span><br><span class="line">    			$<span class="keyword">this</span>-&gt;fireCacheEvent(<span class="string">'write'</span>, [$key, $value, $minutes]);</span><br><span class="line">    		&#125;</span><br><span class="line">    	&#125;</span><br><span class="line">vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">array $attributes = array(</span>))</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">        <span class="keyword">if</span>($<span class="keyword">this</span>-&gt;connection == <span class="string">"mongodb"</span>) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;_time_cost_start = microtime(<span class="literal">true</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">		$<span class="keyword">this</span>-&gt;bootIfNotBooted();</span><br><span class="line"></span><br><span class="line">		$<span class="keyword">this</span>-&gt;syncOriginal();</span><br><span class="line"></span><br><span class="line">		$<span class="keyword">this</span>-&gt;fill($attributes);</span><br><span class="line">	&#125;</span><br><span class="line">	public <span class="function"><span class="keyword">function</span> <span class="title">save</span>(<span class="params">array $options = array(</span>))</span></span><br><span class="line"><span class="function">    	</span>&#123;</span><br><span class="line">    	    <span class="keyword">if</span>($<span class="keyword">this</span>-&gt;connection == <span class="string">"mongodb"</span>) &#123;</span><br><span class="line">                $<span class="keyword">this</span>-&gt;_time_cost_start = microtime(<span class="literal">true</span>);</span><br><span class="line">            &#125;</span><br><span class="line">  *<span class="regexp">/</span></span><br><span class="line"><span class="regexp"> 	protected function performInsert(Builder $query, array $options = [])</span></span><br><span class="line"><span class="regexp"> 	&#123;</span></span><br><span class="line"><span class="regexp"> 		if ($this-&gt;fireModelEvent('creating') === false) return false;</span></span><br><span class="line"><span class="regexp"> </span></span><br><span class="line"><span class="regexp"> </span></span><br><span class="line"><span class="regexp"> 		if ($this-&gt;timestamps &amp;&amp; array_get($options, 'timestamps', true))</span></span><br><span class="line"><span class="regexp"> 		&#123;</span></span><br><span class="line"><span class="regexp"> 			$this-&gt;updateTimestamps();</span></span><br><span class="line"><span class="regexp"> 		&#125;</span></span><br><span class="line"><span class="regexp"> </span></span><br><span class="line"><span class="regexp"> 		$attributes = $this-&gt;attributes;</span></span><br><span class="line"><span class="regexp"> </span></span><br><span class="line"><span class="regexp">         $conn = $query-&gt;getModel()-&gt;connection;</span></span><br><span class="line"><span class="regexp">         if($conn == "mongodb") &#123;</span></span><br><span class="line"><span class="regexp">             unset($attributes['_time_cost_start']);</span></span><br><span class="line"><span class="regexp">             unset($attributes['_time_cost_total']);</span></span><br><span class="line"><span class="regexp">         &#125;</span></span><br><span class="line"><span class="regexp">protected function fireModelEvent($event, $halt = true)</span></span><br><span class="line"><span class="regexp">	&#123;</span></span><br><span class="line"><span class="regexp">		if ( ! isset(static::$dispatcher)) return true;</span></span><br><span class="line"><span class="regexp"> </span></span><br><span class="line"><span class="regexp">        /</span>**</span><br><span class="line">         * 将监听事件类名去掉，去掉<span class="class"><span class="keyword">class</span></span></span><br><span class="line"><span class="class">         * 添加<span class="title">mongodb</span>响应时间</span></span><br><span class="line"><span class="class">         */</span></span><br><span class="line"><span class="class">		//$<span class="title">event</span> </span>= <span class="string">"eloquent.&#123;$event&#125;: "</span>.get_class($<span class="keyword">this</span>);</span><br><span class="line">        $event = <span class="string">"eloquent.&#123;$event&#125;"</span>;</span><br><span class="line">		$method = $halt ? <span class="string">'until'</span> : <span class="string">'fire'</span>;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span>($<span class="keyword">this</span>-&gt;connection == <span class="string">"mongodb"</span>) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;_time_cost_total = round((microtime(<span class="literal">true</span>) - $<span class="keyword">this</span>-&gt;_time_cost_start) * <span class="number">1000</span>, <span class="number">2</span>);</span><br><span class="line">        &#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">static</span>::$dispatcher-&gt;$method($event, $<span class="keyword">this</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Mysql event handle</span></span><br><span class="line">           $app[<span class="string">'events'</span>]-&gt;listen(<span class="string">'illuminate.query'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$sql, $binds, $time,$name</span>) <span class="title">use</span> (<span class="params">$app</span>)</span>&#123;</span><br><span class="line">               self::HandleMysqlSysLog($sql, $binds, $time, $name, $app);</span><br><span class="line">           &#125;);</span><br><span class="line">   </span><br><span class="line">           <span class="comment">//MongoDB event handle</span></span><br><span class="line">           $app[<span class="string">'events'</span>]-&gt;listen(<span class="string">'eloquent.saved'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$model</span>) <span class="title">use</span> (<span class="params">$app</span>)</span>&#123;</span><br><span class="line">               self::MongodbSysLog(<span class="string">'saved'</span>, $app, $model);</span><br><span class="line">           &#125;);</span><br><span class="line">           $app[<span class="string">'events'</span>]-&gt;listen(<span class="string">'eloquent.deleted'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$model</span>) <span class="title">use</span> (<span class="params">$app</span>)</span>&#123;</span><br><span class="line">               self::MongodbSysLog(<span class="string">'deleted'</span>, $app, $model);</span><br><span class="line">           &#125;);</span><br><span class="line">   </span><br><span class="line">           <span class="comment">//Memcache event handle</span></span><br><span class="line">           $app[<span class="string">'events'</span>]-&gt;listen(<span class="string">'cache.write'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$key, $value, $time, $total</span>) <span class="title">use</span> (<span class="params">$app</span>)</span>&#123;</span><br><span class="line">               self::MemcacheSysLog(<span class="string">'put'</span>,$key,$total);</span><br><span class="line">           &#125;);</span><br><span class="line">           $app[<span class="string">'events'</span>]-&gt;listen(<span class="string">'cache.delete'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$key, $total</span>) <span class="title">use</span> (<span class="params">$app</span>)</span>&#123;</span><br><span class="line">               self::MemcacheSysLog(<span class="string">'delete'</span>,$key,$total);</span><br><span class="line">           &#125;);</span><br><span class="line">           $app[<span class="string">'events'</span>]-&gt;listen(<span class="string">'cache.hit'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$key, $value, $total</span>) <span class="title">use</span> (<span class="params">$app</span>)</span>&#123;</span><br><span class="line">               self::MemcacheSysLog(<span class="string">'get'</span>,$key,$total);</span><br><span class="line">           &#125;);</span><br><span class="line">           $app[<span class="string">'events'</span>]-&gt;listen(<span class="string">'cache.missed'</span>,<span class="function"><span class="keyword">function</span>(<span class="params">$key, $total</span>) <span class="title">use</span> (<span class="params">$app</span>)</span>&#123;</span><br><span class="line">               self::MemcacheSysLog(<span class="string">'missed'</span>,$key,$total);</span><br><span class="line">           &#125;);</span><br><span class="line">           </span><br><span class="line">  public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">MysqlSysLog</span>(<span class="params">$sql, $binds, $time, $name, $app</span>)</span>&#123;</span><br><span class="line">          foreach ($binds <span class="keyword">as</span> $bind) &#123;</span><br><span class="line">              $sql = preg_replace(<span class="string">'/\?/'</span>, <span class="string">'\''</span> . $bind . <span class="string">'\''</span>, $sql, <span class="number">1</span>);</span><br><span class="line">          &#125;</span><br><span class="line">          <span class="comment">//调用select时先判断使用读库还是写库，而insert/update/delete统一使用写库 Illuminate/Database/Connectors/ConnectionFactory.php Illuminate/Database/Connection.php</span></span><br><span class="line">          $connection = $app[<span class="string">'db'</span>]-&gt;getconnections();</span><br><span class="line">          <span class="keyword">if</span> ($name) &#123;</span><br><span class="line">              <span class="keyword">if</span> (preg_match(<span class="string">'#^select[\s\S]*$#'</span>, $sql, $match)) &#123;</span><br><span class="line">                  $pdo = $connection[$name]-&gt;getreadPdo();</span><br><span class="line">                  $host = explode(<span class="string">' '</span>, $pdo-&gt;getAttribute(\PDO::ATTR_CONNECTION_STATUS))[<span class="number">0</span>]; </span><br><span class="line">                  $server_info = $pdo-&gt;getAttribute(\PDO::ATTR_SERVER_INFO);</span><br><span class="line">              &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                  $pdo = $connection[$name]-&gt;getpdo();</span><br><span class="line">                  $host = explode(<span class="string">' '</span>, $pdo-&gt;getAttribute(\PDO::ATTR_CONNECTION_STATUS))[<span class="number">0</span>];</span><br><span class="line">                  $server_info = $pdo-&gt;getAttribute(\PDO::ATTR_SERVER_INFO);</span><br><span class="line">              &#125;</span><br><span class="line">  </span><br><span class="line">              $data = [</span><br><span class="line">                  <span class="string">'rs'</span> =&gt; <span class="string">'mysql://'</span> . $host.<span class="string">':'</span>.$connection[$name]-&gt;getconfig(<span class="string">'port'</span>) . <span class="string">'/'</span> . $connection[$name]-&gt;getconfig(<span class="string">'name'</span>),</span><br><span class="line">                   <span class="string">'cmd'</span> =&gt; $sql,</span><br><span class="line">                  <span class="string">'output_content'</span> =&gt; [</span><br><span class="line">                      <span class="string">'database'</span> =&gt; $connection[$name]-&gt;getconfig(<span class="string">'database'</span>),</span><br><span class="line">                      <span class="string">'host'</span> =&gt; $host,</span><br><span class="line">                      <span class="string">'name'</span> =&gt; $connection[$name]-&gt;getconfig(<span class="string">'name'</span>),</span><br><span class="line">                      <span class="string">'port'</span> =&gt; $connection[$name]-&gt;getconfig(<span class="string">'port'</span>),</span><br><span class="line">                      <span class="string">'driver'</span> =&gt; $connection[$name]-&gt;getconfig(<span class="string">'driver'</span>),</span><br><span class="line">                      <span class="string">'server_info'</span> =&gt; $server_info</span><br><span class="line">                  ],</span><br><span class="line">                  <span class="string">'t_total'</span> =&gt; $time</span><br><span class="line">              ]; </span><br><span class="line">              self::SaveSysLog($data);</span><br><span class="line">          &#125; </span><br><span class="line">      &#125;         </span><br><span class="line">      public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">MemcacheSysLog</span>(<span class="params">$cmd, $key, $total</span>)</span>&#123;</span><br><span class="line">              <span class="keyword">if</span>(app(<span class="string">'cache'</span>)-&gt;getStore() <span class="keyword">instanceof</span> \Illuminate\Cache\MemcachedStore)&#123;</span><br><span class="line">                  $servers = implode(<span class="string">'/'</span>, array_keys(app(<span class="string">'cache'</span>)-&gt;getMemcached()-&gt;getStats()));</span><br><span class="line">                  $data = [</span><br><span class="line">                      <span class="string">'rs'</span> =&gt; <span class="string">'mc://'</span> . $servers,</span><br><span class="line">                       <span class="string">'cmd'</span> =&gt; $cmd . <span class="string">' '</span> . $key,</span><br><span class="line">                      <span class="string">'t_total'</span> =&gt; $total,</span><br><span class="line">                      <span class="string">'code'</span> =&gt; <span class="number">0</span></span><br><span class="line">                  ];</span><br><span class="line">              &#125;  </span><br><span class="line">       self::SaveSysLog($data);</span><br><span class="line">           &#125;     </span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">MongodbSysLog</span>(<span class="params">$cmd, $app, $model</span>)</span>&#123;</span><br><span class="line">            $connection = $app[<span class="string">'db'</span>]-&gt;getconnections();</span><br><span class="line">            <span class="keyword">if</span>(isset($connection[<span class="string">'mongodb'</span>])) &#123;</span><br><span class="line">                $data = [</span><br><span class="line">                    <span class="string">'rs'</span> =&gt; <span class="string">'Mongo://'</span> . $connection[<span class="string">'mongodb'</span>]-&gt;getconfig(<span class="string">'host'</span>) . <span class="string">':'</span> . $connection[<span class="string">'mongodb'</span>]-&gt;getconfig(<span class="string">'port'</span>) ,</span><br><span class="line">                     <span class="string">'cmd'</span> =&gt; $cmd,</span><br><span class="line">                    <span class="string">'output_content'</span> =&gt; [</span><br><span class="line">                        <span class="string">'database'</span> =&gt; $connection[<span class="string">'mongodb'</span>]-&gt;getconfig(<span class="string">'database'</span>),</span><br><span class="line">                        <span class="string">'host'</span> =&gt; $connection[<span class="string">'mongodb'</span>]-&gt;getconfig(<span class="string">'host'</span>),</span><br><span class="line">                        <span class="string">'port'</span> =&gt; $connection[<span class="string">'mongodb'</span>]-&gt;getconfig(<span class="string">'port'</span>),</span><br><span class="line">                        <span class="string">'driver'</span> =&gt; $connection[<span class="string">'mongodb'</span>]-&gt;getconfig(<span class="string">'driver'</span>)</span><br><span class="line">                    ],</span><br><span class="line">                    <span class="string">'t_total'</span> =&gt; $model-&gt;_time_cost_total,</span><br><span class="line">                    <span class="string">'rs_content'</span> =&gt; $model-&gt;toArray()[<span class="string">'_id'</span>]</span><br><span class="line">                ];</span><br><span class="line">            &#125;  </span><br><span class="line">            </span><br><span class="line">            self::SaveSysLog($data);</span><br><span class="line">        &#125;   </span><br><span class="line">        public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">SaveSysLog</span>(<span class="params">$data</span>)</span>&#123;</span><br><span class="line">                <span class="keyword">if</span>(php_sapi_name()==<span class="string">'cli'</span>)&#123;</span><br><span class="line">                    <span class="keyword">return</span> ;</span><br><span class="line">                &#125;</span><br><span class="line">        </span><br><span class="line">                 openlog(<span class="string">'app'</span>,LOG_PID|LOG_ODELAY,LOG_LOCAL6);</span><br><span class="line">                <span class="keyword">try</span>&#123;</span><br><span class="line">                    syslog(LOG_INFO,json_encode($data));</span><br><span class="line">                &#125; <span class="keyword">catch</span> (\Exception $e) &#123;</span><br><span class="line">                    \Log::info(<span class="string">'syslog_error'</span>, [<span class="string">'data'</span> =&gt; $data]);</span><br><span class="line">                &#125;</span><br><span class="line">                closelog();</span><br><span class="line">            &#125;</span><br><span class="line">            </span><br><span class="line">vendor/laravel/framework/src/Illuminate/Cache/MemcachedStore.php:<span class="number">164</span>            </span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">get</span>(<span class="params">$key</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;start = microtime(<span class="literal">true</span>);</span><br><span class="line">		$value = $<span class="keyword">this</span>-&gt;memcached-&gt;get($<span class="keyword">this</span>-&gt;prefix.$key);</span><br><span class="line">        <span class="comment">//$total = round((microtime(true) - $this-&gt;start) * 1000, 2);</span></span><br><span class="line">        $<span class="keyword">this</span>-&gt;end = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;handleMcSysLog(<span class="string">'get'</span>, $key);</span><br><span class="line">		<span class="keyword">if</span> ($<span class="keyword">this</span>-&gt;memcached-&gt;getResultCode() == <span class="number">0</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="keyword">return</span> $value;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;            </span><br><span class="line">            </span><br><span class="line">     private <span class="function"><span class="keyword">function</span> <span class="title">handleMcSysLog</span>(<span class="params">$cmd, $key</span>)</span></span><br><span class="line"><span class="function">         </span>&#123;</span><br><span class="line">             $total = round(($<span class="keyword">this</span>-&gt;end - $<span class="keyword">this</span>-&gt;start) * <span class="number">1000</span>, <span class="number">2</span>);</span><br><span class="line">             $rs = implode(<span class="string">'/'</span>, array_keys($<span class="keyword">this</span>-&gt;memcached-&gt;getStats()));</span><br><span class="line">             $data = [</span><br><span class="line">                 <span class="string">'rs'</span> =&gt; <span class="string">'mc://'</span> . $rs,</span><br><span class="line">                 <span class="string">'rs_type'</span> =&gt; <span class="number">6</span>,</span><br><span class="line">                 <span class="string">'cmd'</span> =&gt; $cmd . <span class="string">' '</span> . $key,</span><br><span class="line">                 <span class="string">'t_total'</span> =&gt; $total,</span><br><span class="line">                 <span class="string">'code'</span> =&gt; $<span class="keyword">this</span>-&gt;memcached-&gt;getLastErrorCode(),</span><br><span class="line">                 <span class="string">'ext'</span> =&gt; $<span class="keyword">this</span>-&gt;memcached-&gt;getLastErrorMessage()</span><br><span class="line">             ];</span><br><span class="line">             </span><br><span class="line">     </span><br><span class="line">         &#125;</span><br></pre></td></tr></table></figure>
<h3 id="记录Redis运行时间"><a href="#记录Redis运行时间" class="headerlink" title="记录Redis运行时间"></a>记录Redis运行时间</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line">predis</span><br><span class="line"></span><br><span class="line">use \Predis\Command\CommandInterface;</span><br><span class="line">use \Predis\Connection\StreamConnection;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RedisLog</span> <span class="keyword">extends</span> <span class="title">StreamConnection</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    private $tstart = <span class="number">0</span>;</span><br><span class="line">    private $debugBuffer = array();</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">connect</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;tstart = microtime(<span class="literal">true</span>);</span><br><span class="line"></span><br><span class="line">        parent::connect();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">storeDebug</span>(<span class="params">CommandInterface $command, $direction</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $firtsArg = $command-&gt;getArguments();</span><br><span class="line">        $timestamp = round((microtime(<span class="literal">true</span>) - $<span class="keyword">this</span>-&gt;tstart) * <span class="number">1000</span>,<span class="number">2</span>);</span><br><span class="line">        $log = [];</span><br><span class="line">        $log[<span class="string">'cmd'</span>] = $command-&gt;getId();</span><br><span class="line">        $log[<span class="string">'key'</span>] = isset($firtsArg) ?  $firtsArg  : <span class="string">' '</span>;</span><br><span class="line">        $log[<span class="string">'server'</span>] = <span class="string">"$direction $this"</span>;</span><br><span class="line">        $log[<span class="string">'time'</span>] = $timestamp;</span><br><span class="line">        dump([<span class="string">'rs'</span> =&gt; <span class="string">'redis://'</span> . trim($log[<span class="string">'server'</span>]), <span class="string">'cmd'</span> =&gt; $command-&gt;getId(),   <span class="string">'t_total'</span> =&gt; $timestamp]);</span><br><span class="line"> </span><br><span class="line"><span class="comment">//        $this-&gt;debugBuffer[] = &amp;$log;</span></span><br><span class="line">        unset($log);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">writeRequest</span>(<span class="params">CommandInterface $command</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;tstart = microtime(<span class="literal">true</span>);<span class="comment">//执行命令行的时候重新算 这样就不会叠加</span></span><br><span class="line">        parent::writeRequest($command);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        $this-&gt;storeDebug($command, '-&gt;');</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">readResponse</span>(<span class="params">CommandInterface $command</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $response = parent::readResponse($command);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;storeDebug($command, <span class="string">''</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $response;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getDebugBuffer</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;debugBuffer;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">debug</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $options = array(</span><br><span class="line">            <span class="string">'connections'</span> =&gt;[</span><br><span class="line">                <span class="string">'tcp'</span> =&gt; <span class="string">'\App\Services\RedisLog'</span>,</span><br><span class="line">            ],</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        $client = <span class="keyword">new</span> \Predis\Client(config(<span class="string">'database.redis'</span>), $options);</span><br><span class="line">        $client-&gt;get(<span class="string">'redis:test'</span>);</span><br><span class="line"></span><br><span class="line">        var_export($client-&gt;getConnection());</span><br><span class="line">        <span class="comment">/* OUTPUT:</span></span><br><span class="line"><span class="comment">        array (</span></span><br><span class="line"><span class="comment">          0 =&gt; 'SELECT 15 -&gt; 127.0.0.1:6379 [0.0008s]',</span></span><br><span class="line"><span class="comment">          1 =&gt; 'SELECT 15 &lt;- 127.0.0.1:6379 [0.001s]',</span></span><br><span class="line"><span class="comment">          2 =&gt; 'SET foo -&gt; 127.0.0.1:6379 [0.001s]',</span></span><br><span class="line"><span class="comment">          3 =&gt; 'SET foo &lt;- 127.0.0.1:6379 [0.0011s]',</span></span><br><span class="line"><span class="comment">          4 =&gt; 'GET foo -&gt; 127.0.0.1:6379 [0.0013s]',</span></span><br><span class="line"><span class="comment">          5 =&gt; 'GET foo &lt;- 127.0.0.1:6379 [0.0015s]',</span></span><br><span class="line"><span class="comment">          6 =&gt; 'INFO -&gt; 127.0.0.1:6379 [0.0019s]',</span></span><br><span class="line"><span class="comment">          7 =&gt; 'INFO &lt;- 127.0.0.1:6379 [0.0022s]',</span></span><br><span class="line"><span class="comment">        )</span></span><br><span class="line"><span class="comment">        */</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="记录guggle运行时间"><a href="#记录guggle运行时间" class="headerlink" title="记录guggle运行时间"></a>记录guggle运行时间</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">use Illuminate\Http\Request;</span><br><span class="line">use GuzzleHttp\Event\EmitterInterface;</span><br><span class="line">use GuzzleHttp\Event\BeforeEvent;</span><br><span class="line">use GuzzleHttp\Event\CompleteEvent;</span><br><span class="line">use GuzzleHttp\Event\EndEvent;</span><br><span class="line">use GuzzleHttp\Event\Emitter;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">GuzzleEmitter</span> <span class="keyword">extends</span> <span class="title">Emitter</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$rs_info = <span class="string">'api_time'</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $start = <span class="number">0</span>;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;on(<span class="string">'before'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">BeforeEvent $event, $name</span>) <span class="title">use</span>(<span class="params">&amp;$start, $rs_info</span>)</span>&#123;</span><br><span class="line">            $start = microtime(<span class="literal">true</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">        $<span class="keyword">this</span>-&gt;on(<span class="string">'end'</span>, <span class="function"><span class="keyword">function</span> (<span class="params">EndEvent $event</span>) <span class="title">use</span>(<span class="params">&amp;$start, $rs_info</span>)</span>&#123;</span><br><span class="line">            $url = $event-&gt;gettransferInfo()[<span class="string">'url'</span>];</span><br><span class="line">            $parse = parse_url($url);</span><br><span class="line">            <span class="keyword">if</span> ($event-&gt;getException()) &#123;</span><br><span class="line">                dump([<span class="string">'rs'</span> =&gt; $url, <span class="string">'ext1'</span> =&gt; sprintf(<span class="string">'%s://%s%s'</span>,$parse[<span class="string">'scheme'</span>],$parse[<span class="string">'host'</span>],$parse[<span class="string">'path'</span>]), <span class="string">'cmd'</span> =&gt; $event-&gt;getRequest()-&gt;getmethod(), <span class="string">'code'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'http_code'</span>],   <span class="string">'t_total'</span> =&gt; intval((microtime(<span class="literal">true</span>)-$start)*<span class="number">1000</span>), <span class="string">'p_ip'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'primary_ip'</span>], <span class="string">'req_id'</span>=&gt;defined(<span class="string">'REQUEST_ID'</span>)?(REQUEST_ID):<span class="string">''</span>,]);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                $method=$event-&gt;getRequest()-&gt;getmethod();</span><br><span class="line">                $post_data=($method==<span class="string">'POST'</span>)?((string)$event-&gt;getRequest()-&gt;getBody()):<span class="string">''</span>;</span><br><span class="line">                dump([</span><br><span class="line">                    <span class="string">'rs'</span> =&gt; $url,</span><br><span class="line">                    <span class="string">'cmd'</span> =&gt; $method,</span><br><span class="line">                    <span class="string">'code'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'http_code'</span>],</span><br><span class="line">                     <span class="string">'t_total'</span> =&gt; intval((microtime(<span class="literal">true</span>)-$start)*<span class="number">1000</span>),</span><br><span class="line">                    <span class="string">'p_ip'</span> =&gt; $event-&gt;gettransferInfo()[<span class="string">'primary_ip'</span>],</span><br><span class="line">                    <span class="string">'rs_content'</span>=&gt;$post_data,</span><br><span class="line">                    <span class="string">'ext1'</span> =&gt; sprintf(<span class="string">'%s://%s%s'</span>,$parse[<span class="string">'scheme'</span>],$parse[<span class="string">'host'</span>],$parse[<span class="string">'path'</span>]),</span><br><span class="line">                    <span class="string">'req_id'</span>=&gt;defined(<span class="string">'REQUEST_ID'</span>)?(REQUEST_ID):<span class="string">''</span>,</span><br><span class="line">                ]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="未加密的-Cookie"><a href="#未加密的-Cookie" class="headerlink" title="未加密的 Cookie"></a>未加密的 Cookie</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">默认情况下，Laravel 生成的所有 cookie 都是经过加密和签名的，防止被非法篡改。但 Laravel 也支持不对 cookie 加密，具体做法如下：</span><br><span class="line"></span><br><span class="line">修改 app/Http/Middleware 目录中 App\Http\Middleware\EncryptCookies 中间件的 $except 属性，该属性是个数组，把不需加密的 cookie 名加入到该数组属性中就可以了：</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 不需要被加密的cookies名称</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @var array</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">protected $except = [</span><br><span class="line">    <span class="string">'cookie_name'</span>,</span><br><span class="line">];</span><br></pre></td></tr></table></figure>
<h3 id="无法获取-cookie"><a href="#无法获取-cookie" class="headerlink" title="无法获取 cookie"></a>无法获取 cookie</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取cookie的三种尝试</span></span><br><span class="line">Cookie::get(<span class="string">'cookie_name'</span>);</span><br><span class="line">request()-&gt;cookie(<span class="string">'cookie_name'</span>);</span><br><span class="line">$request-&gt;cookie(<span class="string">'cookie_name'</span>)；</span><br><span class="line"><span class="comment">//返回值均为null laravel 设置 cookie 是加密之后的，而我获取的 cookie 是外来 cookie，由于解密失败，所以怎么获取都是空</span></span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/31939</span></span><br><span class="line"><span class="comment">//添加到cookie名称到 App\Http\Middleware\EncryptCookies 的 排除名单 中：</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">EncryptCookies</span> <span class="keyword">extends</span> <span class="title">Middleware</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    protected $except = [</span><br><span class="line">      <span class="string">'cookie_name'</span>,</span><br><span class="line">      ];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-Excel-导出-csv"><a href="#Laravel-Excel-导出-csv" class="headerlink" title="Laravel Excel 导出 csv"></a>Laravel Excel 导出 csv</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Laravel Excel <span class="number">2.0</span> 的版本和 <span class="number">3.0</span> 的版本有很大的区别，使用 composer 加载的时候要注意版本，<span class="number">3.0</span> 和 <span class="number">2.0</span> 的语法基本不同了</span><br><span class="line">composer <span class="built_in">require</span> maatwebsite/excel ~<span class="number">2.1</span></span><br><span class="line">php artisan vendor:publish --provider=<span class="string">"Maatwebsite\Excel\ExcelServiceProvider"</span>  </span><br><span class="line">config/excel.php 中找到 csv，把里面的 use_bom=&gt;<span class="literal">false</span> 改为 use_bom=&gt;<span class="literal">true</span>，这样导出的 csv 文件就会有 bom 头，不会出现乱码。</span><br><span class="line"></span><br><span class="line">Excel::create(iconv(<span class="string">'UTF-8'</span>, <span class="string">'GBK'</span>, <span class="string">'文章点赞数据'</span>),<span class="function"><span class="keyword">function</span> (<span class="params">$excel</span>) <span class="title">use</span>(<span class="params">$params</span>)</span>&#123;</span><br><span class="line">        $excel-&gt;sheet(<span class="string">'score'</span>,<span class="function"><span class="keyword">function</span> (<span class="params">$sheet</span>) <span class="title">use</span>(<span class="params">$params</span>)</span>&#123;</span><br><span class="line">            $sheet-&gt;rows($params);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;)-&gt;<span class="keyword">export</span>(<span class="string">'csv'</span>);</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32002</span></span><br><span class="line">按照 semver https:<span class="comment">//semver.org/lang/zh-CN/ 的规则，主版本号本来就是做不兼容 API 修改的，升级了主版本号，语法当然不一样了。这不算是坑，只是你不了解而已。</span></span><br><span class="line">https:<span class="comment">//github.com/rap2hpoutre/fast-excel  maatwebsite/excel 感觉太耗内存了</span></span><br></pre></td></tr></table></figure>
<h3 id="项目开发规范"><a href="#项目开发规范" class="headerlink" title="项目开发规范"></a>项目开发规范</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">常用的 HTTP 动词有下面五个。</span><br><span class="line"></span><br><span class="line">GET（SELECT）：从服务器取出资源（一项或多项）。</span><br><span class="line">POST（CREATE）：在服务器新建一个资源。</span><br><span class="line">PUT（UPDATE）：在服务器更新资源（客户端提供改变后的完整资源）。</span><br><span class="line">PATCH（UPDATE）：在服务器更新资源（客户端提供改变的属性）。</span><br><span class="line">DELETE（DELETE）：从服务器删除资源。</span><br><span class="line">所有 URL 必须 是全小写，所有的路由都要用 name</span><br><span class="line"></span><br><span class="line"> Route::get(<span class="string">'user/create'</span>,<span class="string">'UserController@create'</span>)-&gt;name(<span class="string">'admin.user.create'</span>);</span><br><span class="line">一个是打开 create 页面的请求，一个是 create 数据的请求，其中的路由规范如下：</span><br><span class="line"></span><br><span class="line">路由采用 reseful API 的设计规范</span><br><span class="line"></span><br><span class="line">        Route::get(<span class="string">'member/create'</span>, <span class="string">'MemberController@create'</span>)-&gt;name(<span class="string">'admin.member.create'</span>);</span><br><span class="line">        Route::post(<span class="string">'member/store'</span>, <span class="string">'MemberController@store'</span>)-&gt;name(<span class="string">'admin.member.store'</span>);</span><br><span class="line">ORM 的设计规范</span><br><span class="line"></span><br><span class="line">框架的 ORM 只单纯的做模型关联使用，另外新建 Repsitory 层做数据库的逻辑处理，</span><br><span class="line"></span><br><span class="line">Repsitory 的命名采用驼峰法命令规范，比如</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">namespace App\Repository\Admin;</span><br><span class="line">use App\Models\User;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserRepository</span> </span>&#123;</span><br><span class="line">Controller 的设计规范</span><br><span class="line"></span><br><span class="line">数据验证在 request 层进行处理，保证进行控制器的数据是干净的。</span><br><span class="line">对于数据的处理则调用对应的 Repsitory 层处理，控制器只做响应和返回对应的数据。https:<span class="comment">//learnku.com/articles/32141</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-中-Facade"><a href="#Laravel-中-Facade" class="headerlink" title="Laravel 中 Facade"></a>Laravel 中 Facade</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">Route::get(<span class="string">'/'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> view(<span class="string">'welcome'</span>);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">这里 Route 就是用 Facade 实现类方法 get 的静态调用。</span><br><span class="line"></span><br><span class="line">Laravel 中的 Facade 解决类什么问题？</span><br><span class="line"></span><br><span class="line">在 php 中，很多情况都需要使用一个容器获取到所有的对象，然后再调用改对象的方法，这样在编写代码的时候就会看到很长的一个调用链。例如:</span><br><span class="line">在 Yii2 中，几乎所有的系统类都是在 app 容器当中，对这些系统类进行操作都需要执行 Yii::$app-&gt;route 获取到类实例，然后在执行方法 Yii::$app-&gt;route-&gt;get()。但是如果用 Facade 实现之后的调用就是 Route::get()。这样的写法是的代码更加简洁。</span><br><span class="line"></span><br><span class="line">Laravel 中 Facade 是怎么实现的？</span><br><span class="line"></span><br><span class="line">思路是通过__callStatic 魔术方法将方法调用代理到实际的对象方法中去。</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">abstract <span class="class"><span class="keyword">class</span> <span class="title">Facade</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getFacadeRoot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">static</span>::resolveFacadeInstance(<span class="keyword">static</span>::getFacadeAccessor());</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">    protected <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">resolveFacadeInstance</span>(<span class="params">$name</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (is_object($name)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $name;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (isset(<span class="keyword">static</span>::$resolvedInstance[$name])) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">static</span>::$resolvedInstance[$name];</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">static</span>::$resolvedInstance[$name] = <span class="keyword">static</span>::$app[$name];</span><br><span class="line">    &#125;</span><br><span class="line">    ...</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getFacadeAccessor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">//子类返回类名</span></span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">__callStatic</span>(<span class="params">$method, $args</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $instance = <span class="keyword">static</span>::getFacadeRoot();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (! $instance) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">'A facade root has not been set.'</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $instance-&gt;$method(...$args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Route</span> <span class="keyword">extends</span> <span class="title">Facade</span></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getFacadeAccessor</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'router'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">根据每个 Facade 中提供的 getFacadeAccessor 返回实际的对象类名，获取类对象。每个类对象一旦创建，就放在一个静态数组中，因此在一次请求中最多只会被创建一次。</span><br><span class="line"></span><br><span class="line">有没有其他的实现方式？</span><br><span class="line"></span><br><span class="line">从上面的代码可以看到，其实核心就是一个静态代理的功能。那么有没有其他的实现方式了呢？</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Facade</span></span>&#123;</span><br><span class="line">    public <span class="keyword">static</span> $instance;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getFacadeRoot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">static</span>::resolveFacadeInstance(<span class="keyword">static</span>::getFacadeAccessor());</span><br><span class="line">    &#125;</span><br><span class="line">    protected <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">resolveFacadeInstance</span>(<span class="params">$name</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (is_object($name)) &#123;</span><br><span class="line">            <span class="keyword">return</span> $name;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (isset(<span class="keyword">static</span>::$instance)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">static</span>::$instance;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">static</span>::$instance = <span class="keyword">static</span>::$app[$name];</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">__callStatic</span>(<span class="params">$method, $args</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $instance = <span class="keyword">static</span>::getFacadeRoot();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (! $instance) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">'A facade root has not been set.'</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> $instance-&gt;$method(...$args);</span><br><span class="line">    &#125; </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Route</span> <span class="keyword">extends</span> <span class="title">Facade</span></span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line">可以看出，上面的方式也能够实现静态代理，类似于 Facade 的功能。都是通过魔术方法实现。作用上，都简化了代码编写。</span><br><span class="line"></span><br><span class="line">两种不同实现方式的区别</span><br><span class="line"></span><br><span class="line">第二种实现方式有一个很大的缺点，那就是必须继承 Facade 类。PHP 本身只能继承一个类，所以第二种实现方式对于一些需要继承其他类的对象是不适合的。https:<span class="comment">//learnku.com/articles/31964</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-默认邮箱登录改成用户名登录"><a href="#Laravel-默认邮箱登录改成用户名登录" class="headerlink" title="Laravel 默认邮箱登录改成用户名登录"></a>Laravel 默认邮箱登录改成用户名登录</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">默认的，Laravel 使用的是 Illuminate\Foundation\Auth\AuthenticatesUsers 这个 trait 完成登录功能的。通过观察 AuthenticatesUsers 的代码，发现下面一段很有意思的代码:</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">validateLogin</span>(<span class="params">Request $request</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;validate($request, [</span><br><span class="line">            $<span class="keyword">this</span>-&gt;username() =&gt; <span class="string">'required|string'</span>,</span><br><span class="line">            <span class="string">'password'</span> =&gt; <span class="string">'required|string'</span>,</span><br><span class="line">        ]);</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">username</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'email'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">可以看到，是应为 trait 里定义了用户名就是 email，所以才会使得验证的时候通过用户邮箱验证。所以我们只需要定义一个 trait，覆盖 AuthenticatesUsers 中的 username() 方法即可实现后端代码通过用户名验证登录。</span><br><span class="line"></span><br><span class="line">新增的 trait 代码</span><br><span class="line"></span><br><span class="line">namespace App\Utils;</span><br><span class="line"></span><br><span class="line">use Illuminate\Foundation\Auth\AuthenticatesUsers <span class="keyword">as</span> LaravelAuthenticatesUsers;</span><br><span class="line">trait AuthenticatesUsers &#123;</span><br><span class="line">    use LaravelAuthenticatesUsers;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">username</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">'name'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">其实还有另一个简单的修改方式，直接在 LoginController 中新增 username() 方法。由于当前定义方法会覆盖 trait 的方法，因此也能达到修改的目的。但是会破坏登录代码的整体一致性，所以最好还是通过新增 trait 的方式实现。</span><br><span class="line"></span><br><span class="line">同时要记得修改前端 blade 文件中对输入参数的验证，然后就可以使用用户名登录了 https:<span class="comment">//learnku.com/articles/32431</span></span><br></pre></td></tr></table></figure>
<h3 id="设置错误等级"><a href="#设置错误等级" class="headerlink" title="设置错误等级"></a>设置错误等级</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"> /vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:<span class="number">12</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">bootstrap</span>(<span class="params">Application $app</span>)</span></span><br><span class="line"><span class="function">	</span>&#123;</span><br><span class="line">		$<span class="keyword">this</span>-&gt;app = $app;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> ($app-&gt;environment(<span class="string">'production'</span>)) &#123;</span><br><span class="line">            error_reporting(E_ERROR);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            error_reporting(<span class="number">-1</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">		set_error_handler([$<span class="keyword">this</span>, <span class="string">'handleError'</span>]);</span><br><span class="line"></span><br><span class="line">		set_exception_handler([$<span class="keyword">this</span>, <span class="string">'handleException'</span>]);</span><br><span class="line"></span><br><span class="line">		register_shutdown_function([$<span class="keyword">this</span>, <span class="string">'handleShutdown'</span>]);</span><br><span class="line"></span><br><span class="line">		<span class="keyword">if</span> ( $app-&gt;environment(<span class="string">'production'</span>))</span><br><span class="line">		&#123;</span><br><span class="line">			ini_set(<span class="string">'display_errors'</span>, <span class="string">'Off'</span>);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<h3 id="图片添加水印"><a href="#图片添加水印" class="headerlink" title="图片添加水印"></a>图片添加水印</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">composer <span class="built_in">require</span> intervention/image</span><br><span class="line">在我的测试图片文件夹 images 里有一张主图 main.png 和一张水印图 watermark.png。</span><br><span class="line"></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">addWatermark</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">   $img = Image::make(public_path(<span class="string">'images/main.png'</span>));    </span><br><span class="line">   $img-&gt;insert(public_path(<span class="string">'watermark.png'</span>),<span class="string">'bottom-right'</span>,<span class="number">10</span>, <span class="number">10</span>); </span><br><span class="line">   $img-&gt;save();</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/32414</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-配置二级目录访问"><a href="#Laravel-配置二级目录访问" class="headerlink" title="Laravel 配置二级目录访问"></a>Laravel 配置二级目录访问</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line">域名 http:<span class="comment">//test.local 正常访问，对应项目地址 E:/Project/Test</span></span><br><span class="line">域名 http:<span class="comment">//test.local/module2 正常访问，对应项目地址 E:/Project/Module2</span></span><br><span class="line">废话不说上配置https:<span class="comment">//learnku.com/articles/32499</span></span><br><span class="line"></span><br><span class="line">Nginx 配置文件 test_local.conf</span><br><span class="line"></span><br><span class="line">server &#123;</span><br><span class="line">    listen       <span class="number">80</span>;</span><br><span class="line">    server_name  test.local;</span><br><span class="line"></span><br><span class="line">    root E:<span class="regexp">/Project/</span>Test;</span><br><span class="line">    index  index.php index.html;</span><br><span class="line"></span><br><span class="line">    # 转发到端口</span><br><span class="line">    location /module2/ &#123;</span><br><span class="line">        proxy_set_header Host $host;</span><br><span class="line">        proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line">        proxy_set_header X-Real-PORT $remote_port;</span><br><span class="line">        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line">        proxy_set_header X-Forwarded-Host $proxy_host;</span><br><span class="line">        proxy_set_header X-Forwarded-Port $proxy_port;</span><br><span class="line">        proxy_pass http:<span class="comment">//127.0.0.1:8081/;</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    location / &#123;</span><br><span class="line">        try_files $uri $uri/ <span class="regexp">/index.php?$query_string;</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp">    location ~ \.php$ &#123;</span></span><br><span class="line"><span class="regexp">        fastcgi_pass   127.0.0.1:9000;</span></span><br><span class="line"><span class="regexp">        fastcgi_index  index.php;</span></span><br><span class="line"><span class="regexp">        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;</span></span><br><span class="line"><span class="regexp">        include        fastcgi_params;</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">server &#123;</span></span><br><span class="line"><span class="regexp">    listen       8081;</span></span><br><span class="line"><span class="regexp">    server_name 127.0.0.1;</span></span><br><span class="line"><span class="regexp">    root E:/</span>Project/Module2;</span><br><span class="line">    index  index.php index.html;</span><br><span class="line"></span><br><span class="line">    location / &#123;</span><br><span class="line">        try_files $uri $uri/ <span class="regexp">/index.php?$query_string;</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp">    location ~ \.php$ &#123;</span></span><br><span class="line"><span class="regexp">        fastcgi_pass   127.0.0.1:9000;</span></span><br><span class="line"><span class="regexp">        fastcgi_index  index.php;</span></span><br><span class="line"><span class="regexp">        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;</span></span><br><span class="line"><span class="regexp">        include        fastcgi_params;</span></span><br><span class="line"><span class="regexp">    &#125;</span></span><br><span class="line"><span class="regexp">&#125;</span></span><br><span class="line"><span class="regexp">module2 的 Laravel 配置 Url</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">.env</span></span><br><span class="line"><span class="regexp"></span></span><br><span class="line"><span class="regexp">APP_URL=http:/</span><span class="regexp">/test.local/m</span>odule2</span><br><span class="line">app/Providers/AppServiceProvider</span><br><span class="line"></span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 用于修正URL生成的链接</span></span><br><span class="line">        app(<span class="string">'url'</span>)-&gt;forceRootUrl(config(<span class="string">'app.url'</span>));</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">register</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="comment">// 用于修正分页的链接</span></span><br><span class="line">        \Illuminate\Pagination\Paginator::currentPathResolver(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">            <span class="keyword">return</span> $<span class="keyword">this</span>-&gt;app[<span class="string">'url'</span>]-&gt;current();</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="Lumen-日志自定义"><a href="#Lumen-日志自定义" class="headerlink" title="Lumen 日志自定义"></a>Lumen 日志自定义</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、在 Providers 目录建立 LogServiceProvider.php 文件 代码如下</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">namespace App\Providers;</span><br><span class="line"></span><br><span class="line">use Carbon\Laravel\ServiceProvider;</span><br><span class="line">use Monolog\Formatter\LineFormatter;</span><br><span class="line">use Monolog\Handler\RotatingFileHandler;</span><br><span class="line">use Monolog\Processor\IntrospectionProcessor;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LogServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">boot</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $handlers[] = (<span class="keyword">new</span> RotatingFileHandler(storage_path(<span class="string">'logs/lumen.log'</span>), <span class="number">0</span>))</span><br><span class="line">            -&gt;pushProcessor(<span class="keyword">new</span> IntrospectionProcessor())</span><br><span class="line">            -&gt;setFormatter(<span class="keyword">new</span> LineFormatter(<span class="literal">null</span>, <span class="literal">null</span>, <span class="literal">true</span>, <span class="literal">true</span>));</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;app[<span class="string">'log'</span>]-&gt;setHandlers($handlers);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">2</span>、在 bootstrap/app.php 注册该服服</span><br><span class="line"></span><br><span class="line">    $app-&gt;register(App\Providers\LogServiceProvider::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"><span class="number">3</span>、使用测试</span><br><span class="line"></span><br><span class="line">    app(<span class="string">'log'</span>)-&gt;info(<span class="string">'我是测试日志'</span>);</span><br><span class="line"><span class="number">4</span>、生成的日志信息</span><br><span class="line"></span><br><span class="line">[<span class="number">2018</span><span class="number">-04</span><span class="number">-11</span> <span class="number">10</span>:<span class="number">25</span>:<span class="number">29.313456</span>] local.INFO: 我是来测试日志的 &#123;<span class="string">"content"</span>:<span class="number">1111</span>&#125; </span><br><span class="line">https:<span class="comment">//learnku.com/articles/32543</span></span><br></pre></td></tr></table></figure>
<h3 id="Lumen-实时记录-SQL"><a href="#Lumen-实时记录-SQL" class="headerlink" title="Lumen 实时记录 SQL"></a>Lumen 实时记录 SQL</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span>、在 Listeners 目录新建 QueryListener.php 文件 代码如下：</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line">namespace App\Listeners;</span><br><span class="line"></span><br><span class="line">use Illuminate\Database\Events\QueryExecuted;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">QueryListener</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">handle</span>(<span class="params">QueryExecuted $event</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (env(<span class="string">'APP_ENV'</span>, <span class="string">'production'</span>) == <span class="string">'local'</span>) &#123;</span><br><span class="line">            $sql = str_replace(<span class="string">'?'</span>, <span class="string">"'%s'"</span>, $event-&gt;sql);</span><br><span class="line">            $log = vsprintf($sql, $event-&gt;bindings);</span><br><span class="line">            $<span class="keyword">this</span>-&gt;put_log(<span class="string">'sql'</span>, $log);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    private <span class="function"><span class="keyword">function</span> <span class="title">put_log</span>(<span class="params">$file = <span class="string">'app'</span>, $content = <span class="string">''</span></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $data = date(<span class="string">'Y-m-d'</span>);</span><br><span class="line">        $cut_line = str_repeat(<span class="string">"-"</span>, <span class="number">100</span>);</span><br><span class="line">        is_dir(storage_path(<span class="string">'logs/sql'</span>)) or mkdir (storage_path(<span class="string">'logs/sql'</span>), <span class="number">0777</span>, <span class="literal">true</span>); <span class="comment">// 文件夹不存在则创建</span></span><br><span class="line">        $content = <span class="string">'['</span> . date(<span class="string">'Y-m-d H:i:s'</span>) . <span class="string">"]"</span> . $content;</span><br><span class="line">        @file_put_contents(storage_path(<span class="string">'logs/sql/'</span> . $file . <span class="string">'-'</span> . $data . <span class="string">'.log'</span>), $content . <span class="string">"\n"</span> . $cut_line . <span class="string">"\n\n"</span>, FILE_APPEND);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">2</span>、在 Providers/EventServiceProvider.php 添加如下代码</span><br><span class="line"></span><br><span class="line">&lt;?php</span><br><span class="line"></span><br><span class="line">namespace App\Providers;</span><br><span class="line"></span><br><span class="line">use Laravel\Lumen\Providers\EventServiceProvider <span class="keyword">as</span> ServiceProvider;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">EventServiceProvider</span> <span class="keyword">extends</span> <span class="title">ServiceProvider</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * The event listener mappings for the application.</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * @var array</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    protected $listen = [</span><br><span class="line"></span><br><span class="line">        <span class="string">'Illuminate\Database\Events\QueryExecuted'</span> =&gt; [</span><br><span class="line">            <span class="string">'App\Listeners\QueryListener'</span></span><br><span class="line">        ],</span><br><span class="line">    ];</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">3</span>、在 bootstrap/app.php 文件中开启 EventServiceProvider 注册即可</span><br><span class="line"></span><br><span class="line"> $app-&gt;register(App\Providers\EventServiceProvider::<span class="class"><span class="keyword">class</span>)</span>;</span><br><span class="line"><span class="number">4</span>、接下来写一个 sql 语句就能在 storage/logs/sql 看到生成的 sql 日志了</span><br><span class="line"></span><br><span class="line">    app(<span class="string">'db'</span>)-&gt;where(<span class="string">'id'</span>, <span class="string">'&gt;'</span>, <span class="string">'5'</span>)-&gt;get();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32540</span></span><br></pre></td></tr></table></figure>
<h3 id="替换-Word-里面变量并导出-PDF"><a href="#替换-Word-里面变量并导出-PDF" class="headerlink" title="替换 Word 里面变量并导出 PDF"></a>替换 Word 里面变量并导出 PDF</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">    composer <span class="built_in">require</span> phpoffice/phpword</span><br><span class="line">use PhpOffice\PhpWord\TemplateProcessor;</span><br><span class="line"> $path = storage_path(<span class="string">'aa.docx'</span>);</span><br><span class="line">        <span class="comment">// 生成world 存放目录</span></span><br><span class="line">        $filePath = storage_path(<span class="string">'contract.docx'</span>);</span><br><span class="line">        <span class="comment">// 声明模板象并读取模板内容</span></span><br><span class="line">        $templateProcessor = <span class="keyword">new</span> TemplateProcessor($path);</span><br><span class="line">        <span class="comment">// 替换模板内容</span></span><br><span class="line">        $templateProcessor-&gt;setValue(<span class="string">'contract'</span>, <span class="string">'北京乙方'</span>);  <span class="comment">// 乙方</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// 生成新的 world</span></span><br><span class="line">        $templateProcessor-&gt;saveAs($filePath);</span><br><span class="line">      apt-get install unoconv</span><br><span class="line">      #如果报错请求服务器语言设置为 LANG=”en_US.UTF-8″</span><br><span class="line">      </span><br><span class="line">      #使用命令把 word 转为 pdf</span><br><span class="line">      unoconv -f pdf aa.docx</span><br><span class="line">      #这个时候在当前目录下就会有一个 aa.pdf 的文件出来</span><br><span class="line">      #但是会发现如果是中文的情况下转出来的 pdf 是乱码该如何解决  </span><br><span class="line">            # 把电脑本机的宋体上传到服务器字体目录下 /usr/share/fonts 新建一个目录 win 或者其它，把中文字体上传到该目录下</span><br><span class="line">            apt-get install mkfontscale  #安装这个工具</span><br><span class="line">            # 进入到/usr/share/fonts/win/ 执行命令</span><br><span class="line">            mkfontscale &amp;&amp; sudo mkfontdir &amp;&amp; sudo fc-cache -fv</span><br><span class="line">            # 然后重启服务器让字体生效</span><br><span class="line">            reboot</span><br><span class="line">            # 最后在执行</span><br><span class="line">            unoconv -f pdf aa.docx</span><br><span class="line">            # 看是不是中文乱码的问题解决了</span><br><span class="line">      使用 php 的执行 shell 的函数来调用该函数自动生成即可</span><br><span class="line">      </span><br><span class="line">      shell_exec(<span class="string">'/usr/binunoconv -f pdf aa.docx'</span>)</span><br></pre></td></tr></table></figure>
<h3 id="主键进行批量更新"><a href="#主键进行批量更新" class="headerlink" title="主键进行批量更新"></a>主键进行批量更新</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 根据Id批量更新数据，可以放在model里面，使得每个model都是调用这个方法</span></span><br><span class="line"><span class="comment">   * @author yezi</span></span><br><span class="line"><span class="comment">   * @param array $multipleData</span></span><br><span class="line"><span class="comment">   * @return bool</span></span><br><span class="line"><span class="comment">   */</span></span><br><span class="line"></span><br><span class="line">  public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">updateBatch</span>(<span class="params">$multipleData = array(</span>))</span></span><br><span class="line"><span class="function">  </span>&#123;</span><br><span class="line">    $tableName = \DB::getTablePrefix() . app(Contract::<span class="class"><span class="keyword">class</span>)-&gt;<span class="title">getTable</span>()</span>;</span><br><span class="line">    <span class="keyword">if</span>(!is_array($multipleData))&#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> CustomValidationException(<span class="string">'must be an array'</span>,<span class="literal">null</span>,<span class="string">'6001'</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    foreach ($multipleData <span class="keyword">as</span> &amp;$row)&#123;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">if</span>(!array_key_exists(<span class="string">'id'</span>,$row))&#123;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">throw</span> <span class="keyword">new</span> CustomValidationException(<span class="string">'参数错误,缺少主键'</span>,<span class="literal">null</span>,<span class="string">'6001'</span>);</span><br><span class="line"></span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      $row[Model::FIELD_UPDATED_AT] = Carbon::now();</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ($tableName &amp;&amp; !empty($multipleData)) &#123;</span><br><span class="line"></span><br><span class="line">      $updateColumn  = array_keys($multipleData[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">      $referenceColumn = $updateColumn[<span class="number">0</span>];</span><br><span class="line"></span><br><span class="line">      unset($updateColumn[<span class="number">0</span>]);</span><br><span class="line"></span><br><span class="line">      $whereIn = <span class="string">""</span>;</span><br><span class="line"></span><br><span class="line">      $q = <span class="string">"UPDATE "</span> . $tableName . <span class="string">" SET "</span>;</span><br><span class="line"></span><br><span class="line">      foreach ($updateColumn <span class="keyword">as</span> $uColumn) &#123;</span><br><span class="line"></span><br><span class="line">        $q .= $uColumn . <span class="string">" = CASE "</span>;</span><br><span class="line"></span><br><span class="line">        foreach ($multipleData <span class="keyword">as</span> $data) &#123;</span><br><span class="line"></span><br><span class="line">          $q .= <span class="string">"WHEN "</span> . $referenceColumn . <span class="string">" = "</span> . $data[$referenceColumn] . <span class="string">" THEN '"</span> . $data[$uColumn] . <span class="string">"' "</span>;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        $q .= <span class="string">"ELSE "</span> . $uColumn . <span class="string">" END, "</span>;</span><br><span class="line"></span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      foreach ($multipleData <span class="keyword">as</span> $data) &#123;</span><br><span class="line"></span><br><span class="line">        $whereIn .= <span class="string">"'"</span> . $data[$referenceColumn] . <span class="string">"', "</span>;</span><br><span class="line"></span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      $q = rtrim($q, <span class="string">", "</span>) . <span class="string">" WHERE "</span> . $referenceColumn . <span class="string">" IN ("</span> . rtrim($whereIn, <span class="string">', '</span>) . <span class="string">")"</span>;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">return</span> \DB::update(\DB::raw($q));</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  &#125;https:<span class="comment">//geixue.com/blogs/channels/laravel/laravel-shares-a-way-to-do-bulk-updates-based-on-the-primary-key-qjhqc5</span></span><br></pre></td></tr></table></figure>
<h3 id="Laradock-中使用-beanstalkd"><a href="#Laradock-中使用-beanstalkd" class="headerlink" title="Laradock 中使用 beanstalkd"></a>Laradock 中使用 beanstalkd</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">docker-compose up -d beanstalkd</span><br><span class="line">composer <span class="built_in">require</span> pda/pheanstalk</span><br><span class="line">更改 .env 文件</span><br><span class="line"></span><br><span class="line">设置 QUEUE_CONNECTION 为 beanstalkd, 设置 beanstalkd 为默认队列驱动。</span><br><span class="line">新增配置项 QUEUE_HOST=beanstalkd，代表使用 laradock 中 beanstalkd 的 host 以及端口，端口默认值为 <span class="number">11300</span>。</span><br><span class="line">QUEUE_HOST=beanstalkd </span><br><span class="line">QUEUE_CONNECTION=beanstalkd</span><br><span class="line">更改 config/queue.php 文件</span><br><span class="line"></span><br><span class="line">修改 connections 下面的 beanstalkd 数组中 host 的值为 env(<span class="string">'QUEUE_HOST'</span>, <span class="string">'localhost'</span>)，意思就是默认使用 .env 文件中定义的 QUEUE_HOST 值</span><br><span class="line"></span><br><span class="line"><span class="string">'connections'</span> =&gt; [</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line"></span><br><span class="line">    <span class="string">'beanstalkd'</span> =&gt; [</span><br><span class="line">        <span class="string">'driver'</span> =&gt; <span class="string">'beanstalkd'</span>,</span><br><span class="line">        <span class="string">'host'</span> =&gt; env(<span class="string">'QUEUE_HOST'</span>, <span class="string">'localhost'</span>),</span><br><span class="line">        <span class="string">'queue'</span> =&gt; <span class="string">'default'</span>,</span><br><span class="line">        <span class="string">'retry_after'</span> =&gt; <span class="number">90</span>,</span><br><span class="line">        <span class="string">'block_for'</span> =&gt; <span class="number">0</span>,</span><br><span class="line">    ],</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">    .</span><br><span class="line">],</span><br><span class="line">使用 beanstalkd_console 从 Web 界面管理您的队列</span><br><span class="line"></span><br><span class="line">运行 Beanstalkd 控制台容器</span><br><span class="line">docker-compose up -d beanstalkd-<span class="built_in">console</span></span><br><span class="line">访问 http:<span class="comment">//localhost:2080/  https://learnku.com/articles/32366</span></span><br></pre></td></tr></table></figure>
<h3 id="注册了多少条路由"><a href="#注册了多少条路由" class="headerlink" title="注册了多少条路由"></a>注册了多少条路由</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ php artisan route:list | wc -l | awk <span class="string">'&#123;print $1 - 4&#125;'</span></span><br><span class="line">获取到上面的统计到的行数再减去 <span class="number">4</span>（因为 list 中包含了 <span class="number">3</span> 行制表符 和 <span class="number">1</span> 行表头），最终得到了你的路由数量</span><br></pre></td></tr></table></figure>
<h3 id="Laravel-ORM-SQL-语句查询"><a href="#Laravel-ORM-SQL-语句查询" class="headerlink" title="Laravel ORM SQL 语句查询"></a>Laravel ORM SQL 语句查询</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">在 app/Providers/AppServiceProvider 中的 boot 方法中新增以下代码：</span><br><span class="line"></span><br><span class="line">\Illuminate\Database\Query\Builder::macro(<span class="string">'sql'</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    $bindings = $<span class="keyword">this</span>-&gt;getBindings();</span><br><span class="line">    $sql = str_replace(<span class="string">'?'</span>,<span class="string">'%s'</span>,$<span class="keyword">this</span>-&gt;toSql());</span><br><span class="line">    <span class="keyword">return</span> vsprintf($sql, $bindings);</span><br><span class="line">&#125;);</span><br><span class="line">\Illuminate\Database\Eloquent\Builder::macro(<span class="string">'sql'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> ($<span class="keyword">this</span>-&gt;getQuery()-&gt;sql());</span><br><span class="line">&#125;);</span><br><span class="line">二、使用方式</span><br><span class="line"></span><br><span class="line">查询带参数的 SQL 语句可在 ORM 后调用方法</span><br><span class="line"></span><br><span class="line"><span class="comment">// 正确用法</span></span><br><span class="line">User::query()-&gt;where(<span class="string">'id'</span>, <span class="number">1</span>)-&gt;sql();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 错误用法</span></span><br><span class="line">User::query()-&gt;where(<span class="string">'id'</span>, <span class="number">1</span>)-&gt;get()-&gt;sql();</span><br><span class="line">User::query()-&gt;where(<span class="string">'id'</span>, <span class="number">1</span>)-&gt;first()-&gt;sql();</span><br><span class="line">https:<span class="comment">//learnku.com/articles/33484</span></span><br></pre></td></tr></table></figure>
<h3 id="cache"><a href="#cache" class="headerlink" title="cache"></a>cache</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">use Illuminate\Support\Facades\Cache;</span><br><span class="line"></span><br><span class="line">Cache::get(<span class="string">'xxx'</span>);</span><br><span class="line">app(<span class="string">'Illuminate\Contracts\Cache\Factory'</span>)-&gt;get(<span class="string">'xxx'</span>);</span><br><span class="line">app(<span class="string">'Illuminate\Cache\CacheManager'</span>)-&gt;get(<span class="string">'xxx'</span>);</span><br><span class="line">app(<span class="string">'cache'</span>)-&gt;get(<span class="string">'xxx'</span>);</span><br><span class="line">app()[<span class="string">'cache'</span>]-&gt;get(<span class="string">'xxx'</span>);</span><br><span class="line">cache(<span class="string">'xxx'</span>);</span><br><span class="line">   <span class="function"><span class="keyword">function</span> <span class="title">cache</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="built_in">arguments</span> = func_get_args();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (empty($<span class="built_in">arguments</span>)) &#123;</span><br><span class="line">            <span class="keyword">return</span> app(<span class="string">'cache'</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (is_string($<span class="built_in">arguments</span>[<span class="number">0</span>])) &#123;</span><br><span class="line">            <span class="keyword">return</span> app(<span class="string">'cache'</span>)-&gt;get(...$<span class="built_in">arguments</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (! is_array($<span class="built_in">arguments</span>[<span class="number">0</span>])) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> Exception(</span><br><span class="line">                <span class="string">'When setting a value in the cache, you must pass an array of key / value pairs.'</span></span><br><span class="line">            );</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (! isset($<span class="built_in">arguments</span>[<span class="number">1</span>])) &#123;</span><br><span class="line">            <span class="keyword">throw</span> <span class="keyword">new</span> Exception(</span><br><span class="line">                <span class="string">'You must specify an expiration time when setting a value in the cache.'</span></span><br><span class="line">            );</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> app(<span class="string">'cache'</span>)-&gt;put(key($<span class="built_in">arguments</span>[<span class="number">0</span>]), reset($<span class="built_in">arguments</span>[<span class="number">0</span>]), $<span class="built_in">arguments</span>[<span class="number">1</span>]);</span><br><span class="line">        https:<span class="comment">//learnku.com/articles/33628</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-的请求是怎么到达控制器"><a href="#Laravel-的请求是怎么到达控制器" class="headerlink" title="Laravel 的请求是怎么到达控制器"></a>Laravel 的请求是怎么到达控制器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="分类列表无限分类"><a href="#分类列表无限分类" class="headerlink" title="分类列表无限分类"></a>分类列表无限分类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">public <span class="keyword">static</span> <span class="function"><span class="keyword">function</span> <span class="title">getTree</span>(<span class="params">$data,$pId,$level = <span class="number">0</span></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">static</span> $list = [];</span><br><span class="line">        foreach ($data <span class="keyword">as</span> $key =&gt; $value)&#123;</span><br><span class="line">            <span class="comment">//第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点</span></span><br><span class="line">            <span class="keyword">if</span> ($value[<span class="string">'parent_id'</span>] == $pId)&#123;</span><br><span class="line">                <span class="comment">//父节点为根节点的节点,级别为0，也就是第一级</span></span><br><span class="line">                $value[<span class="string">'level'</span>] = $level;</span><br><span class="line">                $value[<span class="string">'display_name'</span>] =$value[<span class="string">'displayname'</span>];</span><br><span class="line">                $value[<span class="string">'displayname'</span>] = str_repeat(<span class="string">'━━'</span>, $value[<span class="string">'level'</span>]).$value[<span class="string">'displayname'</span>];</span><br><span class="line"></span><br><span class="line">                <span class="comment">//把数组放到list中</span></span><br><span class="line">                $list[] = $value;</span><br><span class="line">                <span class="comment">//把这个节点从数组中移除,减少后续递归消耗</span></span><br><span class="line">                unset($data[$key]);</span><br><span class="line">                <span class="comment">//开始递归,查找父ID为该节点ID的节点,级别则为原级别+1</span></span><br><span class="line">                self::getTree($data, $value[<span class="string">'id'</span>], $level+<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> $list;</span><br><span class="line">    &#125;</span><br><span class="line">优雅的树形数据结构管理包,基于Closure Table模式设计. https:<span class="comment">//github.com/jiaxincui/closure-table</span></span><br></pre></td></tr></table></figure>
<h3 id="获取路由"><a href="#获取路由" class="headerlink" title="获取路由"></a>获取路由</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">$routeCollection = \Route::getRoutes();</span><br><span class="line"></span><br><span class="line">foreach ($routeCollection <span class="keyword">as</span> $value) &#123;</span><br><span class="line">    $routes[] = [$value-&gt;getPath(),$value-&gt;methods()[<span class="number">0</span>], $value-&gt;getActionName()];</span><br><span class="line">&#125;</span><br><span class="line">Route::get(<span class="string">'routes'</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    $routeCollection = Route::getRoutes();</span><br><span class="line"></span><br><span class="line">    echo <span class="string">"&lt;table style='width:100%'&gt;"</span>;</span><br><span class="line">        echo <span class="string">"&lt;tr&gt;"</span>;</span><br><span class="line">            echo <span class="string">"&lt;td width='10%'&gt;&lt;h4&gt;HTTP Method&lt;/h4&gt;&lt;/td&gt;"</span>;</span><br><span class="line">            echo <span class="string">"&lt;td width='10%'&gt;&lt;h4&gt;Route&lt;/h4&gt;&lt;/td&gt;"</span>;</span><br><span class="line">            echo <span class="string">"&lt;td width='10%'&gt;&lt;h4&gt;Name&lt;/h4&gt;&lt;/td&gt;"</span>;</span><br><span class="line">            echo <span class="string">"&lt;td width='70%'&gt;&lt;h4&gt;Corresponding Action&lt;/h4&gt;&lt;/td&gt;"</span>;</span><br><span class="line">        echo <span class="string">"&lt;/tr&gt;"</span>;</span><br><span class="line">        foreach ($routeCollection <span class="keyword">as</span> $value) &#123;</span><br><span class="line">            echo <span class="string">"&lt;tr&gt;"</span>;<span class="comment">//sina/vendor/laravel/framework/src/Illuminate/Routing/Route.php:919</span></span><br><span class="line">                echo <span class="string">"&lt;td&gt;"</span> . $value-&gt;getMethods()[<span class="number">0</span>] . <span class="string">"&lt;/td&gt;"</span>;</span><br><span class="line">                echo <span class="string">"&lt;td&gt;"</span> . $value-&gt;getPath() . <span class="string">"&lt;/td&gt;"</span>;</span><br><span class="line">                echo <span class="string">"&lt;td&gt;"</span> . $value-&gt;getName() . <span class="string">"&lt;/td&gt;"</span>;</span><br><span class="line">                echo <span class="string">"&lt;td&gt;"</span> . $value-&gt;getActionName() . <span class="string">"&lt;/td&gt;"</span>;</span><br><span class="line">            echo <span class="string">"&lt;/tr&gt;"</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    echo <span class="string">"&lt;/table&gt;"</span>;</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">https:<span class="comment">//stackoverflow.com/questions/18394891/how-to-get-a-list-of-registered-route-paths-in-laravel</span></span><br><span class="line">https:<span class="comment">//stackoverflow.com/questions/15955295/displaying-registered-routes-in-laravel</span></span><br><span class="line"></span><br><span class="line">php artisan route:list  sina/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteListCommand.php:<span class="number">14</span></span><br></pre></td></tr></table></figure>
<h3 id="自动将空字符串转为-null"><a href="#自动将空字符串转为-null" class="headerlink" title="自动将空字符串转为 null"></a>自动将空字符串转为 null</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">laravel 有一个 ConvertEmptyStringsToNull 中间件，自动将空字符串转为 <span class="literal">null</span>。</span><br><span class="line">在 App\Http\Kernel.php 中，注释了这个中间件就好了。</span><br><span class="line"> QueryException In Connection.php line <span class="number">664</span> :</span><br><span class="line"> SQLSTATE[<span class="number">23000</span>]: Integrity constraint violation: <span class="number">1048</span> Column <span class="string">'judge_group'</span> cannot be <span class="literal">null</span></span><br><span class="line"> </span><br><span class="line"> 打印下 Request 返回的数据，Laravel 有个中间件会把 <span class="string">' '</span> 转为 <span class="literal">null</span>。https:<span class="comment">//learnku.com/laravel/t/35052</span></span><br></pre></td></tr></table></figure>
<h3 id="Laravel-5-5-升级到-6-0"><a href="#Laravel-5-5-升级到-6-0" class="headerlink" title="Laravel 5.5 升级到 6.0"></a>Laravel 5.5 升级到 6.0</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">使用 phpredis 替代 predis</span><br><span class="line"></span><br><span class="line">Laravel 开始推荐使用 phpredis 来代替 predis。</span><br><span class="line"></span><br><span class="line">所以要记得先安装 phpredis, 然后在 config/app.php 中去掉 Redis 别名</span><br><span class="line">https:<span class="comment">//learnku.com/articles/35056</span></span><br></pre></td></tr></table></figure>
<h3 id="API-和-后台接口分离"><a href="#API-和-后台接口分离" class="headerlink" title="API 和 后台接口分离"></a>API 和 后台接口分离</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">发现路由数量变多后影响到了性能，这个时候需要区别不同服务器去加载不同的路由。</span><br><span class="line"></span><br><span class="line">如何去别不同的服务器区别环境，但是又要区别是生产环境。</span><br><span class="line">可以使用 app()-&gt;environment(); 方法实现，生产环境和测试环境的区别。</span><br><span class="line"></span><br><span class="line">查看代码后发现可以使用更多的方法。</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取或检查当前应用程序环境。</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * @return string|bool</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">environment</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 返回传递给函数的参数数量</span></span><br><span class="line">    <span class="keyword">if</span> (func_num_args() &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="comment">// 如果第一个参数是数组就去第一个，不是的话取全部的。</span></span><br><span class="line">        $patterns = is_array(func_get_arg(<span class="number">0</span>)) ? func_get_arg(<span class="number">0</span>) : func_get_args();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> Str::is($patterns, $<span class="keyword">this</span>[<span class="string">'env'</span>]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> $<span class="keyword">this</span>[<span class="string">'env'</span>];</span><br><span class="line">&#125;</span><br><span class="line">Str::is 函数判断给定的字符串是否匹配给定的模式。星号 * 可以用来表示通配符：</span><br><span class="line"></span><br><span class="line"># 判断在 API 环境</span><br><span class="line">app()-&gt;environment(<span class="string">"production.api"</span>);</span><br><span class="line"># 判断在 ADMIN 环境</span><br><span class="line">app()-&gt;environment(<span class="string">"production.admin"</span>);</span><br><span class="line"># 判断在所有环境</span><br><span class="line">app()-&gt;environment(<span class="string">"production.*"</span>);</span><br><span class="line">修改 RouteServiceProvider 文件</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Define the routes for the application.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">map</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">// 公共路由</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (app()-&gt;environment(<span class="string">'production.api'</span>)) &#123;</span><br><span class="line">        # production api 路由</span><br><span class="line">        $<span class="keyword">this</span>-&gt;mapApiRoutes();</span><br><span class="line">    &#125; elseif (app()-&gt;environment(<span class="string">'production.admin'</span>)) &#123;</span><br><span class="line">        # production admin 路由</span><br><span class="line">        $<span class="keyword">this</span>-&gt;mapAdminRoutes();</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        # local testing stanging 环境下加载所有路由</span><br><span class="line">        $<span class="keyword">this</span>-&gt;mapApiRoutes();</span><br><span class="line"></span><br><span class="line">        $<span class="keyword">this</span>-&gt;mapAdminRoutes();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/35099</span></span><br></pre></td></tr></table></figure>
<h3 id="laravel路由分割"><a href="#laravel路由分割" class="headerlink" title="laravel路由分割"></a>laravel路由分割</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line">Route::group([<span class="string">'middleware'</span> =&gt; [<span class="string">'web'</span>]], <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    Route::group([<span class="string">'prefix'</span> =&gt; <span class="string">'user'</span>, <span class="string">'namespace'</span> =&gt; <span class="string">'User'</span>], <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span> app_path(<span class="string">'Http/Routes/user.php'</span>);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    Route::group([<span class="string">'prefix'</span> =&gt; <span class="string">'admin'</span>, <span class="string">'namespace'</span> =&gt; <span class="string">'Admin'</span>], <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span> app_path(<span class="string">'Http/Routes/admin.php'</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">    Route::group([<span class="string">'prefix'</span> =&gt; <span class="string">'api'</span>, <span class="string">'namespace'</span> =&gt; <span class="string">'Home'</span>], <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="built_in">require</span> app_path(<span class="string">'Http/Routes/homeapi.php'</span>);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;);</span><br><span class="line">app/Providers/RouteServiceProvider.php 的 map 方法中可以如下定义：</span><br><span class="line">public <span class="function"><span class="keyword">function</span> <span class="title">map</span>(<span class="params">Router $router</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $router-&gt;group([<span class="string">'namespace'</span> =&gt; $<span class="keyword">this</span>-&gt;namespace], <span class="function"><span class="keyword">function</span> (<span class="params">$router</span>) </span>&#123;</span><br><span class="line">        <span class="comment">//require app_path('Http/routes.php');</span></span><br><span class="line">        foreach (glob(app_path(<span class="string">'Http//Routes'</span>) . <span class="string">'/*.php'</span>) <span class="keyword">as</span> $file) &#123;</span><br><span class="line">            $<span class="keyword">this</span>-&gt;app-&gt;make(<span class="string">'App\\Http\\Routes\\'</span> . basename($file, <span class="string">'.php'</span>))-&gt;map($router);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/laravel/t/2484/the-best-way-to-split-routesphp-laravel-routing-file</span></span><br><span class="line">php artisan route:cache</span><br><span class="line"></span><br><span class="line">   public <span class="function"><span class="keyword">function</span> <span class="title">map</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;mapWebRoutes();</span><br><span class="line">        $<span class="keyword">this</span>-&gt;mapApiRoutes();</span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">mapWebRoutes</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Route::group([</span><br><span class="line">            <span class="string">'middleware'</span> =&gt; <span class="string">'web'</span>,</span><br><span class="line">            <span class="string">'namespace'</span> =&gt; $<span class="keyword">this</span>-&gt;namespace,</span><br><span class="line">        ], <span class="function"><span class="keyword">function</span> (<span class="params">$router</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">require</span> base_path(<span class="string">'routes/web.php'</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    protected <span class="function"><span class="keyword">function</span> <span class="title">mapApiRoutes</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        Route::group([</span><br><span class="line">            <span class="string">'middleware'</span> =&gt; [<span class="string">'api'</span>, <span class="string">'auth:api'</span>],</span><br><span class="line">            <span class="string">'namespace'</span> =&gt; $<span class="keyword">this</span>-&gt;namespace,</span><br><span class="line">            <span class="string">'prefix'</span> =&gt; <span class="string">'api'</span>,</span><br><span class="line">        ], <span class="function"><span class="keyword">function</span> (<span class="params">$router</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">require</span> base_path(<span class="string">'routes/api.php'</span>);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">http:<span class="comment">//www.xiaot123.com/post/laravel_route</span></span><br><span class="line"></span><br><span class="line">按照URL</span><br><span class="line"><span class="keyword">if</span> (Str::is(<span class="string">'api.*'</span>, request()-&gt;server(<span class="string">'HTTP_HOST'</span>) ?? <span class="string">''</span>)) &#123;</span><br><span class="line">      $<span class="keyword">this</span>-&gt;mapApiRoutes();</span><br><span class="line">&#125; elseif (Str::is(<span class="string">'admin.*'</span>, request()-&gt;server(<span class="string">'HTTP_HOST'</span>) ?? <span class="string">''</span>)) &#123;</span><br><span class="line">      $<span class="keyword">this</span>-&gt;mapAdminRoutes();</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      $<span class="keyword">this</span>-&gt;mapApiRoutes();</span><br><span class="line"></span><br><span class="line">      $<span class="keyword">this</span>-&gt;mapAdminRoutes();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><a href="https://github.com/AlexStack/Laravel-CMS" target="_blank" rel="noopener">Laravel 项目的简单 CMS</a></p>
<p><a href="https://learnku.com/docs/the-laravel-way/5.6/Tao-1-1/2925" target="_blank" rel="noopener">Laravel 之道</a></p>
<p>Laravel 代码生成器 <a href="https://learnku.com/laravel/t/34772" target="_blank" rel="noopener">https://learnku.com/laravel/t/34772</a> <a href="https://github.com/GooGee/Entity-Builder" target="_blank" rel="noopener">https://github.com/GooGee/Entity-Builder</a></p>
<p><a href="https://github.com/yanlongma/docker-lnmp" target="_blank" rel="noopener">使用 Docker LNMP 部署 PHP 开发环境 </a></p>
<p><a href="https://learnku.com/laravel/t/34780" target="_blank" rel="noopener">PHP-fpm MongoDB 连接数 </a></p>
<p><a href="https://github.com/sheaxiang/shea" target="_blank" rel="noopener"> Laravel 源码写的一个简易框架</a></p>
<p><a href="https://learnku.com/articles/34628" target="_blank" rel="noopener">轻松部署 Laravel 应用 </a></p>
<p>接近免费的 SSL 证书 <a href="https://www.digital-sign.com.cn/register" target="_blank" rel="noopener">https://www.digital-sign.com.cn/register</a></p>
<p>一键生成 https 根域名证书更方便，推荐 certbot-auto 脚本</p>
<p><a href="https://github.com/mylxsw/wizard" target="_blank" rel="noopener">Laravel 的开源api文档管理系统 Wizard </a></p>
<p><a href="https://learnku.com/articles/34879" target="_blank" rel="noopener">CentOS7 轻松部署 Laravel 应用</a></p>
<p><a href="https://github.com/peinhu/aetherupload-laravel" target="_blank" rel="noopener">超大文件上传</a></p>
<p><a href="https://learnku.com/articles/34568" target="_blank" rel="noopener">使用 AetherUpload 视频上传过程</a></p>
<p><a href="https://github.com/kphcdr/axeadmin-laravel" target="_blank" rel="noopener">axeadmin 让你的 Laravel 快速拥有一个后台</a></p>
<p><a href="https://learnku.com/articles/34123" target="_blank" rel="noopener">Laravel 的请求是怎么到达控制器</a></p>
<p><a href="https://github.com/eleven26/listen-sql" target="_blank" rel="noopener">控制台实时查看 sql</a></p>
<p><a href="https://learnku.com/articles/33400" target="_blank" rel="noopener">HTTP 协议免费升级到 HTTPS</a></p>
<p><a href="https://github.com/wmhello/laravel_template_with_vue" target="_blank" rel="noopener"> Laravel-echo-server 开发聊天室和客服功能</a></p>
<p><a href="https://learnku.com/docs/the-laravel-way/5.6/Tao-1-1/2925" target="_blank" rel="noopener">laravel之道</a></p>
<p><a href="https://googee.github.io/Entity-Builder/dist/" target="_blank" rel="noopener">Laravel 代码生成工具</a></p>
<p><a href="https://learnku.com/articles/32007#replies" target="_blank" rel="noopener">Laravel 源码阅读 - Eloquent</a></p>
<p><a href="https://github.com/alexeymezenin/laravel-best-practices/blob/master/chinese.md" target="_blank" rel="noopener">laravel最佳实践</a></p>
<p><a href="https://github.com/sweida/laravel-blog-api/" target="_blank" rel="noopener">Laravel+vue 个人博客</a></p>
<p><a href="https://laravelacademy.org/laravel-from-appreciate-to-artisan" target="_blank" rel="noopener">Laravel 从学徒到工匠精校版 </a></p>
<p><a href="https://learnku.com/articles/32333" target="_blank" rel="noopener">服务容器</a></p>
<p><a href="https://github.com/immortalChensm/laravel5.8" target="_blank" rel="noopener">Laravel5.8 版本内核源码注解</a></p>
<p><a href="https://learnku.com/articles/32025" target="_blank" rel="noopener">Laravel-admin 源码分析系列</a></p>
<p><a href="https://learnku.com/articles/32007" target="_blank" rel="noopener">Laravel 源码阅读</a></p>
<p><a href="https://github.com/y-ui/laravel-data-masking" target="_blank" rel="noopener">Laravel 数据库脱敏工具</a></p>
<p><a href="https://github.com/xiaohuilam/laravel/wiki" target="_blank" rel="noopener">Laravel 深入浅出指南  </a></p>
<p><a href="https://github.com/immortalChensm/laravel-work" target="_blank" rel="noopener">Laravel5.5LTS 版本源码内核阅读注解</a></p>
<p><a href="https://leoyang90.gitbooks.io/laravel-source-analysis/content/" target="_blank" rel="noopener">laravel 源码详解https://github.com/LeoYang90/laravel-source-analysis</a></p>
<p><a href="https://github.com/shanyul/photosLive" target="_blank" rel="noopener">Laravel + Vue + Oss 搭建的图片墙</a></p>
<p><a href="https://github.com/tangtanglove/fullstack-backend" target="_blank" rel="noopener">基于 Laravel + Ant-design-pro 界面美观漂亮的后台</a></p>
<p><a href="https://learnku.com/articles/32025" target="_blank" rel="noopener">Laravel-admin 安装分析</a></p>
<p><a href="https://github.com/imnotdoubi/laravel-admin.git" target="_blank" rel="noopener">电子商务后台系统</a></p>
<p><a href="https://learnku.com/laravel/t/31585" target="_blank" rel="noopener">基于laravel的博客</a></p>
<p><a href="https://github.com/immortalChensm/laravel5.8" target="_blank" rel="noopener">laravel5.8版本源码分析</a></p>
<p><a href="https://learnku.com/laravel/t/30996" target="_blank" rel="noopener">基于 adminLTE3、Laravel、swoole 的微服务管理平台</a></p>
<p><a href="https://learnku.com/articles/25922#topnav" target="_blank" rel="noopener">Laravel-snappy 生成 PDF 踩坑记录</a></p>
<p><a href="https://learnku.com/articles/30878" target="_blank" rel="noopener">docker/laradock 安装</a></p>
<p><a href="https://learnku.com/articles/30858" target="_blank" rel="noopener">Laravel 融合 Markdown</a></p>
<p><a href="https://notes.lzis.me/notes/53" target="_blank" rel="noopener">Laravel框架关键技术解析</a></p>
<p><a href="https://learnku.com/articles/26864" target="_blank" rel="noopener">Laravel Markdown Blog</a></p>
<p><a href="https://github.com/yybawang/Adoc" target="_blank" rel="noopener">laravel markdown api文档</a></p>
<p><a href="https://learnku.com/articles/30607" target="_blank" rel="noopener"> markdown 来撰写文档</a></p>
<p><a href="https://learnku.com/articles/30812" target="_blank" rel="noopener">Laravel 融合 Elasticsearch/Algolia</a></p>
<p><a href="https://learnku.com/articles/22514" target="_blank" rel="noopener">Laravel Artisan 命令大全</a></p>
<p><a href="https://learnku.com/laravel/t/30061" target="_blank" rel="noopener">Laravel 底层分析系列文章：生命周期</a></p>
<p><a href="http://youyou-tech.com/2019/05/30/%E5%A4%A7%E8%AF%9D%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91%E7%9A%84%E5%A5%87%E6%8A%80%E6%B7%AB%E5%B7%A7/" target="_blank" rel="noopener">大话后端开发高高并发的奇技淫巧</a></p>
<p><a href="https://github.com/medz/cors" target="_blank" rel="noopener"> PHP 跨域的 CORS 中间件</a></p>
<p><a href="https://github.com/nfangxu/package-fx-translation" target="_blank" rel="noopener">Laravel 谷歌翻译</a></p>
<p><a href="https://learnku.com/articles/29669" target="_blank" rel="noopener">Laravel 搭建 Composer 包</a></p>
<p><a href="https://learnku.com/articles/29622" target="_blank" rel="noopener"> Laravel 和 layui 包含基础 RBAC 权限的管理后台</a></p>
<p><a href="https://learnku.com/laravel/t/29566" target="_blank" rel="noopener">生命周期和容器 Container</a></p>
<p><a href="https://github.com/maatwebsite/Laravel-Excel" target="_blank" rel="noopener">Supercharged Excel exports and imports in Laravel</a></p>
<p><a href="https://learnku.com/articles/25270#replies" target="_blank" rel="noopener">MySQL 避坑宝典</a></p>
<p><a href="https://learnku.com/articles/29490" target="_blank" rel="noopener">文章收藏的扩展包</a></p>
<p><a href="https://learnku.com/articles/29413" target="_blank" rel="noopener">多语言与多时区的解决方案</a></p>
<p><a href="https://github.com/immortalChensm/laravel-work" target="_blank" rel="noopener">laravel框架源码分析注解</a></p>
<p><a href="https://github.com/ww285276792/todo.link" target="_blank" rel="noopener">Laravel 团队任务管理系统</a></p>
<p><a href="https://github.com/tangtanglove/fullstack" target="_blank" rel="noopener">基于 Laravel + Ant-design 界面美观漂亮的后台</a></p>
<p><a href="https://github.com/hongyukeji/laravel-translate/" target="_blank" rel="noopener">Laravel 本地语言包自动翻译插件</a></p>
<p><a href="https://learnku.com/articles/28897" target="_blank" rel="noopener">Laravel+Layim+GatewayWorker workerman 实现实时聊天功能</a></p>
<p><a href="https://github.com/ivothgle/Tampermonkey/blob/master/Laravel-doc-help.user.js" target="_blank" rel="noopener">「Laravel 文档助手」 - 油猴脚本</a></p>
<p><a href="https://github.com/hongyukeji/laravel-sms" target="_blank" rel="noopener">Laravel 短信扩展包</a></p>
<p><a href="https://learnku.com/articles/28432#topnav" target="_blank" rel="noopener">Laravel 中的事件监听</a></p>
<p><a href="https://github.com/alicfeng/laravel-runtime" target="_blank" rel="noopener">基于Laravel构建请求信息日志以及接口智能分析</a></p>
<p><a href="https://learnku.com/docs/laravel-cheatsheet/5.8" target="_blank" rel="noopener">Laravel 5.8 速查表</a></p>
<p><a href="https://github.com/Kamicloud/laravel-unofficial-relations" target="_blank" rel="noopener">Laravel 关联模型扩展</a></p>
<p><a href="https://learnku.com/laravel/t/24449#replies" target="_blank" rel="noopener">Laravel 的第三方平台登录</a></p>
<p><a href="https://learnku.com/articles/28230" target="_blank" rel="noopener">Laravel- 访问设备识别组件-BrowserDetect</a></p>
<p><a href="https://www.laravel-dojo.com/helpful-tools" target="_blank" rel="noopener">Laravel 道场</a></p>
<p><a href="https://learnku.com/articles/19427" target="_blank" rel="noopener">Laravel5.5 支付宝支付</a></p>
<p><a href="http://wangyapeng.me/2019/03/30/laravel-broadcasting/" target="_blank" rel="noopener">Laravel 广播</a></p>
<p><a href="http://wangyapeng.me/2019/01/13/upgrad-laravel-5.5-to-5.7-log/" target="_blank" rel="noopener">Laravel 5.5 升级到 5.7 小记</a></p>
<p><a href="https://www.microsoft.com/en-us/download/details.aspx?id=21138" target="_blank" rel="noopener">生成chm文档</a></p>
<p><a href="https://learnku.com/articles/19859#topnav" target="_blank" rel="noopener">深入研究 Laravel 源码第二天</a></p>
<p><a href="https://github.com/iiDestiny/dependency-injection" target="_blank" rel="noopener">依赖注入扩展包</a></p>
<p><a href="https://github.com/php-casbin/php-casbin" target="_blank" rel="noopener"> PHP 权限管理框架 Casbin</a></p>
<p><a href="https://github.com/foryoufeng/laravel-generator" target="_blank" rel="noopener">laravel 代码生成器 2.0</a></p>
<p><a href="https://learnku.com/blog/Wi1dcard/tags/easy-deployment-of-laravel-applications_50034" target="_blank" rel="noopener">轻松部署 Laravel 应用</a></p>
<p><a href="https://divinglaravel.com/" target="_blank" rel="noopener">深入了解 Laravel 的工作机制</a></p>
<p><a href="https://learnku.com/articles/27934#reply87814" target="_blank" rel="noopener">Laravel 中 IoC 容器</a></p>
<p><a href="https://learnku.com/docs/the-laravel-way/5.6/Tao-1-1/2925" target="_blank" rel="noopener">laravel之道系列</a></p>
<p><a href="https://github.com/kevinyan815/Learning_Laravel_Kernel" target="_blank" rel="noopener">Laravel核心代码学习</a></p>
<p><a href="https://learnku.com/laravel/t/14204/so-what-is-this-register-for" target="_blank" rel="noopener">laravel服务容器</a></p>
<p><a href="https://learnku.com/laravel/t/10781/who-can-compare-the-life-cycle-of-laravel-to-a-more-vivid-image" target="_blank" rel="noopener">Laravel 的生命周期比喻的更形象一点</a></p>
<p><a href="https://learnku.com/articles/26282#topnav" target="_blank" rel="noopener">paypal PHP-sdk 支付 / 回调 / 退款全流程</a></p>
<p><a href="https://github.com/SadCreeper/laravel-react-blog" target="_blank" rel="noopener">Laravel 5.5 和 React 的个人博客系统</a></p>
<p><a href="https://learnku.com/articles/27716" target="_blank" rel="noopener">Laravel 从零单排系列教程</a></p>
<p><a href="https://github.com/eddy8/lightCMS/blob/master/app/functions.php#L65-L103" target="_blank" rel="noopener">基于 Tire 树的敏感词检测</a></p>
<p><a href="https://learnku.com/articles/22307#topnav" target="_blank" rel="noopener">QQ 第三方登录</a></p>
<p><a href="https://learnku.com/articles/27712#topnav" target="_blank" rel="noopener">PHP 有限状态机</a></p>
<p><a href="https://github.com/godruoyi/gblog" target="_blank" rel="noopener">laravel vue博客</a></p>
<p><a href="https://learnku.com/articles/18704" target="_blank" rel="noopener">Voyager 的使用及二次开发</a></p>
<p><a href="https://learnku.com/articles/26344" target="_blank" rel="noopener"> Composer 自动加载源码</a></p>
<p><a href="https://learnku.com/articles/26735#topnav" target="_blank" rel="noopener">GitLab CI 踩坑</a></p>
<p><a href="https://learnku.com/articles/19868#topnav" target="_blank" rel="noopener">小白折腾服务器（一）：docker 搭建 lnmp+ 使用 deployer 部署</a></p>
<p><a href="https://learnku.com/laravel/t/9365/teach-you-to-write-modern-php-code-without-using-a-framework" target="_blank" rel="noopener">不使用框架的情况下也能写出现代化 PHP 代码</a></p>
<p><a href="https://learnku.com/laravel/t/26103" target="_blank" rel="noopener">基于 Laravel 的 CMS</a></p>
<p><a href="https://github.com/Adldap2/Adldap2-Laravel" target="_blank" rel="noopener">Laravel LDAP 包</a></p>
<p><a href="https://learnku.com/articles/27824#reply87460" target="_blank" rel="noopener">PhpSpreadsheet 简单 Excel 导入导出</a></p>
<p><a href="https://learnku.com/articles/25130" target="_blank" rel="noopener">valet 切换 PHP 版本</a></p>
<p><a href="https://github.com/cknow/laravel-money" target="_blank" rel="noopener">Laravel Money 金钱单位格式化</a></p>
<p><a href="https://learnku.com/articles/27806#topnav" target="_blank" rel="noopener">Laravel 中自定义用户登录的数据表users</a></p>
<p><a href="https://learnku.com/articles/14145/rely-on-inversion-control-inversion-ioc-container-dependency-injection#topnav" target="_blank" rel="noopener">浅析依赖倒转、控制反转、IoC 容器、依赖注入</a></p>
<p><a href="https://learnku.com/articles/22769#topnav" target="_blank" rel="noopener">用语言 (非代码) 说清楚 IoC 到底是什么</a></p>
<p><a href="https://1024casts.com/topics/21" target="_blank" rel="noopener">PHP中的依赖注入（DI）容器</a></p>
<p><a href="https://learnku.com/laravel/t/27647#e9289b" target="_blank" rel="noopener">十五个常用的 Laravel 集合</a></p>
<p><a href="https://learnku.com/articles/19195#topnav" target="_blank" rel="noopener">Laravel 服务容器，IoC,DI</a></p>
<p><a href="https://learnku.com/articles/17638" target="_blank" rel="noopener">Laravel 服务容器、服务提供器、契约实例讲解</a></p>
<p><a href="https://learnku.com/articles/19195" target="_blank" rel="noopener">Laravel 服务容器，IoC,DI</a></p>
<p><a href="https://learnku.com/articles/27606#topnav" target="_blank" rel="noopener">《提问的智慧》小测验</a></p>
<p><a href="https://learnku.com/articles/27623" target="_blank" rel="noopener">Laravel 使用 Elasticsearch 全局搜索</a></p>
<p><a href="http://worthly.cn/2018/05/27/algorithm-test/" target="_blank" rel="noopener">PHP实现的一些算法例子</a></p>
<p><a href="http://worthly.cn/2018/06/02/computer-network-reading-notes/" target="_blank" rel="noopener">计算机网络读书笔记</a></p>
<p><a href="http://worthly.cn/2017/12/19/php-regular-preg-match-matching-length-limit/" target="_blank" rel="noopener">正则preg_match匹配长度</a></p>
<p><a href="https://learnku.com/laravel/t/27590" target="_blank" rel="noopener">优酷 YouKu OAuth2 第三方登录库</a></p>
<p><a href="https://github.com/changeweb/Unifiedtransform" target="_blank" rel="noopener">开源学校管理系统</a></p>
<p><a href="https://learnku.com/articles/27556" target="_blank" rel="noopener">超全的设计模式简介</a></p>
<p><a href="https://www.tangshuang.net/1682.html" target="_blank" rel="noopener">如何应对服务器压力</a></p>
<p><a href="https://www.tangshuang.net/6192.html" target="_blank" rel="noopener">数据库中用一个值来保存多种情况：二进制和按位异或</a></p>
<p><a href="https://www.tangshuang.net/1693.html" target="_blank" rel="noopener">在阿里云上搭建自己的git服务器</a></p>
<p><a href="https://learnku.com/articles/27446" target="_blank" rel="noopener">RabbitMQ 的应用场景以及基本原理介绍</a></p>
<p><a href="https://www.tangshuang.net/1774.html" target="_blank" rel="noopener">编译安装nginx1.9.7+php7.0.0</a></p>
<p><a href="https://learnku.com/articles/25947" target="_blank" rel="noopener"> Laravel 开发 API 更得心应手</a></p>
<p><a href="https://learnku.com/articles/22616" target="_blank" rel="noopener">别再使用 JWT 作为 Session 系统</a></p>
<p><a href="https://www.tangshuang.net/4100.html" target="_blank" rel="noopener">同一公司下多产品的用户权限管理系统设计</a></p>
<p><a href="https://github.com/xx19941215/light-tips" target="_blank" rel="noopener">数据结构和算法</a></p>
<p><a href="https://www.tangshuang.net/2794.html" target="_blank" rel="noopener">PHP中关于时间（戳）、时区</a></p>
<p><a href="https://github.com/peinhu/AetherUpload-Laravel" target="_blank" rel="noopener">大文件上传扩展</a></p>
<p><a href="https://learnku.com/laravel/t/26674" target="_blank" rel="noopener">Laravel 微信小程序后端搭建</a></p>
<p><a href="https://tianyong90.com/2019/03/10/yong-algolia-docsearch-qing-song-shi-xian-wen-dang-quan-zhan-sou-suo/#algolia-docsearch-的基本原理和主要优势" target="_blank" rel="noopener"> Algolia DocSearch 轻松实现文档全站搜索</a></p>
<p><a href="https://learnku.com/articles/26987" target="_blank" rel="noopener">SOLID 设计原则</a></p>
<p><a href="https://blog.yiranzai.cn/posts/991/" target="_blank" rel="noopener">MySQL分组查询TOP N的实践和踩坑</a></p>
<p><a href="https://learnku.com/articles/25111#topnav" target="_blank" rel="noopener">不要在循环体中使用 array_merge ()</a></p>
<p><a href="https://github.com/uuk020/logistics" target="_blank" rel="noopener">查询物流信息的扩展包</a></p>
<p><a href="https://learnku.com/laravel/t/27041" target="_blank" rel="noopener">PHPStrom 转 VSCode 折腾记录</a></p>
<p><a href="https://learnku.com/articles/19195" target="_blank" rel="noopener">【看完就懂】Laravel 服务容器，IoC,DI</a></p>
<p><a href="https://learnku.com/articles/25760" target="_blank" rel="noopener">整理 PC 端微信扫码支付全过程 — easywechat + Laravel 5.8</a></p>
<p><a href="https://learnku.com/laravel/t/24161" target="_blank" rel="noopener">Laravel Excel 的五个隐藏功能</a></p>
<p><a href="https://learnku.com/articles/22094" target="_blank" rel="noopener">woann-chat 基于 laravelS 和 layim 的聊天系统</a></p>
<p><a href="https://learnku.com/laravel/t/19023" target="_blank" rel="noopener">Laravel-Excel 3.0 文档</a></p>
<p><a href="https://learnku.com/articles/19589" target="_blank" rel="noopener"> Laravel 进阶教程</a></p>
<p><a href="https://learnku.com/articles/18637" target="_blank" rel="noopener">Laravel Redis 广播 实例</a></p>
<p><a href="https://learnku.com/laravel/t/26281" target="_blank" rel="noopener">基于 The 996 Prohibited License 的项目Laravel-admin-select2</a></p>
<p><a href="https://learnku.com/articles/22046" target="_blank" rel="noopener">处理 API 数据格式</a></p>
<p><a href="https://learnku.com/articles/26733" target="_blank" rel="noopener">Laravel 项目全自动接口管理</a></p>
<p><a href="https://learnku.com/articles/26710#topnav" target="_blank" rel="noopener">Auth 2.0 的一种简单解释</a></p>
<p><a href="https://dmmylove.cn/articles/97" target="_blank" rel="noopener">Laravel 从零单排系列教程 </a></p>
<p><a href="https://learnku.com/articles/24219" target="_blank" rel="noopener">Laravel 5.7 和 JSON Web 令牌（tymon/jwt-auth） - 用户认证</a></p>
<p><a href="https://learnku.com/articles/26343#topnav" target="_blank" rel="noopener">生产环境的 Composer</a></p>
<p><a href="https://learnku.com/laravel/wikis/27707" target="_blank" rel="noopener">输出 SQL 语句</a></p>
<p><a href="https://learnku.com/laravel/t/27688" target="_blank" rel="noopener">Laravel Passport 为你的 REST API 增加用户认证功能</a></p>
<p><a href="https://learnku.com/articles/25111#topnav" target="_blank" rel="noopener">不要在循环体中使用 array_merge ()</a></p>
<p><a href="https://haoruijie.art/2019/04/18/the-secret-of-throttle.html" target="_blank" rel="noopener">Laravel API throttle 限流原理分析</a></p>
<p><a href="https://learnku.com/articles/27389" target="_blank" rel="noopener">Laravel 登录原理剖析</a></p>
<p><a href="https://learnku.com/laravel/wikis/25515" target="_blank" rel="noopener">Laravel 安装和开发环境</a></p>
<p><a href="https://learnku.com/articles/25858" target="_blank" rel="noopener">Cookie 禁用了，Session 还能用吗</a></p>
<p><a href="https://github.com/OMGZui/DesignPattern" target="_blank" rel="noopener">设计模式</a></p>
<p><a href="https://learnku.com/articles/25204" target="_blank" rel="noopener">PHP 详细面试总结</a></p>
<p><a href="https://github.com/baijunyao/design-patterns" target="_blank" rel="noopener">php设计模式 https://baijunyao.com/</a></p>
<p><a href="https://learnku.com/docs/php-design-patterns/2018" target="_blank" rel="noopener">《PHP 设计模式全集 2018》</a></p>
<p><a href="https://learnku.com/articles/26825" target="_blank" rel="noopener">Laravel Markdown Blog</a></p>
<p><a href="https://learnku.com/articles/24982" target="_blank" rel="noopener">设计模式超级简单的解释</a></p>
<p><a href="https://learnku.com/articles/26864#topnav" target="_blank" rel="noopener">Laravel Markdown Blog learnku.net </a></p>
<p><a href="https://learnku.com/articles/26850" target="_blank" rel="noopener">PHP 开发环境安装配置Laradock</a></p>
<p><a href="https://github.com/valiner/identicon-avatar" target="_blank" rel="noopener">头像自动生成</a></p>
<p><a href="https://learnku.com/articles/20990" target="_blank" rel="noopener"> Laravel-admin 集成 Markdown 编辑器</a></p>
<p><a href="https://learnku.com/articles/26180" target="_blank" rel="noopener">swoole 邮件系统</a></p>
<p><a href="https://learnku.com/laravel/t/22814" target="_blank" rel="noopener">Laravel 测试之 —— PHPUnit 入门教程</a></p>
<p><a href="https://learnku.com/articles/26343" target="_blank" rel="noopener">composer 解析</a></p>
<p><a href="https://learnku.com/articles/24217" target="_blank" rel="noopener">Laravel 5.7 撸了一个论坛</a></p>
<p><a href="https://learnku.com/articles/27539" target="_blank" rel="noopener">Laravel 文档助手」 - 油猴脚本</a></p>
<p><a href="https://learnku.com/articles/27029#topnav" target="_blank" rel="noopener">全新搭建 PHP 开发环境</a></p>
<p><a href="https://learnku.com/articles/25444" target="_blank" rel="noopener">编写具有描述性的 RESTful API </a></p>
<p><a href="https://learnku.com/articles/25983" target="_blank" rel="noopener"> Laravel-China 课程文章同步发布</a></p>
<p><a href="https://learnku.com/articles/26686" target="_blank" rel="noopener">代理与反向代理、负载均衡和缓存</a></p>
<p><a href="https://learnku.com/articles/20311" target="_blank" rel="noopener">Laravel5.5 使用 Elasticsearch 做引擎</a></p>
<p><a href="https://learnku.com/laravel/t/22639" target="_blank" rel="noopener">Laravel Eloquent 小技巧</a></p>
<p><a href="https://learnku.com/articles/18697" target="_blank" rel="noopener">Laravel Cron 定时任务 “跳坑” 点</a></p>
<p><a href="https://learnku.com/articles/25798" target="_blank" rel="noopener">Laravel + Laravel-echo + EasyWeChat 实现微信扫码登录</a></p>
<p><a href="https://learnku.com/articles/20247" target="_blank" rel="noopener">你可能不太需要 Laravel-Excel</a></p>
<p><a href="https://learnku.com/articles/21812#99dc86" target="_blank" rel="noopener">Oss Flysystem 扩展</a></p>
<p><a href="https://learnku.com/articles/21522" target="_blank" rel="noopener">Laravel5.6 整合 RabbitMQ 消息队列</a></p>
<p><a href="https://learnku.com/laravel/t/26110" target="_blank" rel="noopener">Laravel 集合（Collection）的基础用法</a></p>
<p><a href="https://learnku.com/articles/20031" target="_blank" rel="noopener">『 OAuth2.0』 猴子都能懂的图解</a></p>
<p><a href="https://learnku.com/laravel/t/22473" target="_blank" rel="noopener"> Laravel 的消息通知系统</a></p>
<p><a href="https://learnku.com/laravel/t/25013" target="_blank" rel="noopener"> Laravel 项目中使用 Elasticsearch 做引擎</a></p>
<p><a href="https://learnku.com/articles/17867" target="_blank" rel="noopener">简聊 Session 与 Token 身份验证</a></p>
<p><a href="https://learnku.com/laravel/t/24767" target="_blank" rel="noopener">六个值得参考的 Laravel 开源项目</a></p>
<p><a href="https://learnku.com/articles/19878" target="_blank" rel="noopener">Laravel 启动流程分析</a></p>
<p><a href="https://learnku.com/articles/25208" target="_blank" rel="noopener">Lumen/Laravel 集成 GatewayWorker (Workerman)，实现简单的聊天系统.</a></p>
<p><a href="https://www.debuginn.cn/study-laravel-1-setting.html" target="_blank" rel="noopener">Laravel踩坑日记之基本配置及Demo</a></p>
<p><a href="https://github.com/Jasongzj/laravel-qcloud-image" target="_blank" rel="noopener">腾讯云智 AI 图像服务的 Laravel SDK</a></p>
<p><a href="https://vien.tech/article/134" target="_blank" rel="noopener">基于Laravel支持markdown的博客Vien Blog</a></p>
<p><a href="https://learnku.com/articles/28578" target="_blank" rel="noopener">Laravel 团队任务管理系统</a></p>
<p><a href="https://learnku.com/articles/27539" target="_blank" rel="noopener">「Laravel 文档助手」 - 油猴脚本</a></p>
<p><a href="https://learnku.com/articles/28637" target="_blank" rel="noopener"> 队列原理分析</a></p>
<p><a href="https://github.com/tsmliyun/laravel_quick_admin" target="_blank" rel="noopener">Laravel + H-ui 搭建后台管理系统</a></p>
<p><a href="https://pinapp.github.io/laravel-shou-ce" target="_blank" rel="noopener">laravel手册</a></p>
<p><a href="https://laravelcode.cn/docs/5.8" target="_blank" rel="noopener">基于 larecipe 做的 Laravel 速查表</a></p>
<p><a href="https://github.com/ratchetphp/Pawl" target="_blank" rel="noopener">Laravel 中连接 Webscoket</a></p>
<p><a href="https://github.com/aoxiang594/infyom-laravel-generator-simple-document" target="_blank" rel="noopener">自动生成 CURD</a></p>
<p><a href="https://github.com/wmhello/laravel_template_with_vue" target="_blank" rel="noopener">laravel5.5和vue.js结合的前后端分离项目模板websocket聊天室</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/laravel/" rel="tag"># laravel</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/03/26/redis-记录/" rel="next" title="redis 记录">
                <i class="fa fa-chevron-left"></i> redis 记录
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/04/26/linux-笔记/" rel="prev" title="linux 笔记">
                linux 笔记 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#事件的使用Laravel-启动流程分析"><span class="nav-number">1.</span> <span class="nav-text">事件的使用Laravel 启动流程分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cron-timeout"><span class="nav-number">2.</span> <span class="nav-text">cron timeout</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#杀死过期进程"><span class="nav-number">3.</span> <span class="nav-text">杀死过期进程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#每个文章的前10条评论一同查询出来"><span class="nav-number">4.</span> <span class="nav-text">每个文章的前10条评论一同查询出来</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-集合"><span class="nav-number">5.</span> <span class="nav-text">Laravel 集合</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#队列执行失败反复执行"><span class="nav-number">6.</span> <span class="nav-text">队列执行失败反复执行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实现Schemaless"><span class="nav-number">7.</span> <span class="nav-text">实现Schemaless</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PhpSpreadsheet-导出图片到-Excel"><span class="nav-number">8.</span> <span class="nav-text">PhpSpreadsheet 导出图片到 Excel</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-集合的-“when”-方法"><span class="nav-number">9.</span> <span class="nav-text">Laravel 集合的 “when” 方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Jwtauth-自定义认证头信息"><span class="nav-number">10.</span> <span class="nav-text">Jwtauth 自定义认证头信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#ajax-实现跨域"><span class="nav-number">11.</span> <span class="nav-text">ajax 实现跨域</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PDO-防止-sql-注入"><span class="nav-number">12.</span> <span class="nav-text">PDO 防止 sql 注入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Eloquent-关系中使用-orderBy"><span class="nav-number">13.</span> <span class="nav-text">Eloquent 关系中使用 orderBy()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#开闭原则"><span class="nav-number">14.</span> <span class="nav-text">开闭原则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#内存占用高，速度就会提高"><span class="nav-number">15.</span> <span class="nav-text">内存占用高，速度就会提高</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#关联模型字段取别名查询不出数据"><span class="nav-number">16.</span> <span class="nav-text">关联模型字段取别名查询不出数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#敏感词过滤算法"><span class="nav-number">17.</span> <span class="nav-text">敏感词过滤算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#curl-https"><span class="nav-number">18.</span> <span class="nav-text">curl https</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-Excel3-0"><span class="nav-number">19.</span> <span class="nav-text">Laravel-Excel3.0</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据填充功能生成中文测试数据"><span class="nav-number">20.</span> <span class="nav-text">数据填充功能生成中文测试数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#intervention-image-坑"><span class="nav-number">21.</span> <span class="nav-text">intervention/image 坑</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#puppeteer-采集异步加载的网页内容"><span class="nav-number">22.</span> <span class="nav-text">puppeteer 采集异步加载的网页内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP时区"><span class="nav-number">23.</span> <span class="nav-text">PHP时区</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#You-have-new-mail-in-var-spool-mail-root"><span class="nav-number">24.</span> <span class="nav-text">You have new mail in /var/spool/mail/root</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#对二维数组进行排序"><span class="nav-number">25.</span> <span class="nav-text">对二维数组进行排序</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#mysql-orderby-limit-翻页数据重复"><span class="nav-number">26.</span> <span class="nav-text">mysql orderby limit 翻页数据重复</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#消息队列处理高并发"><span class="nav-number">27.</span> <span class="nav-text">消息队列处理高并发</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取这两个字符串前面的相同部分"><span class="nav-number">28.</span> <span class="nav-text">获取这两个字符串前面的相同部分</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#树形结构算法"><span class="nav-number">29.</span> <span class="nav-text">树形结构算法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP进程分支设计"><span class="nav-number">30.</span> <span class="nav-text">PHP进程分支设计</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#shell重启服务"><span class="nav-number">31.</span> <span class="nav-text">shell重启服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-IoC-服务容器类管理工具"><span class="nav-number">32.</span> <span class="nav-text">Laravel IoC 服务容器类管理工具</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel函数"><span class="nav-number">33.</span> <span class="nav-text">laravel函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#路由为不区分大小写"><span class="nav-number">34.</span> <span class="nav-text">路由为不区分大小写</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数组扁平化"><span class="nav-number">35.</span> <span class="nav-text">数组扁平化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#phpexcel科学计数法"><span class="nav-number">36.</span> <span class="nav-text">phpexcel科学计数法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHPExcel-在-PHP7-0-以上版本报错"><span class="nav-number">37.</span> <span class="nav-text">PHPExcel 在 PHP7.0 以上版本报错</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#foreach"><span class="nav-number">38.</span> <span class="nav-text">foreach</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#tap"><span class="nav-number">39.</span> <span class="nav-text">tap</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#artisan日志-root-权限解决"><span class="nav-number">40.</span> <span class="nav-text">artisan日志 root 权限解决</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#英文字符占-0-5-个，中文字符占-1-个"><span class="nav-number">41.</span> <span class="nav-text">英文字符占 0.5 个，中文字符占 1 个</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#bas64简历"><span class="nav-number">42.</span> <span class="nav-text">bas64简历</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#PHP-排名算法支持重复排名"><span class="nav-number">43.</span> <span class="nav-text">PHP 排名算法支持重复排名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多个-Laravel-应用-queue-队列执行时会互串"><span class="nav-number">44.</span> <span class="nav-text">多个 Laravel 应用 queue 队列执行时会互串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#修改器"><span class="nav-number">45.</span> <span class="nav-text">修改器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#响应宏原理"><span class="nav-number">46.</span> <span class="nav-text">响应宏原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#队列优先级的一个坑"><span class="nav-number">47.</span> <span class="nav-text">队列优先级的一个坑</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#合成图片"><span class="nav-number">48.</span> <span class="nav-text">合成图片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#http-code-204"><span class="nav-number">49.</span> <span class="nav-text">http code 204</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#json-encode-中文"><span class="nav-number">50.</span> <span class="nav-text">json_encode 中文</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#升级至Laravel-5-7报错setTrustedProxies-must-be-of-the-type-integer"><span class="nav-number">51.</span> <span class="nav-text">升级至Laravel 5.7报错setTrustedProxies() must be of the type integer</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#openssl-encrypt-expects-parameter-1-to-be-string"><span class="nav-number">52.</span> <span class="nav-text">openssl_encrypt() expects parameter 1 to be string</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#多库下的DB-transaction-事务失效"><span class="nav-number">53.</span> <span class="nav-text">多库下的DB::transaction()事务失效</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#执行时间和内存消耗"><span class="nav-number">54.</span> <span class="nav-text">执行时间和内存消耗</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#速查表"><span class="nav-number">55.</span> <span class="nav-text">速查表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-Excel"><span class="nav-number">56.</span> <span class="nav-text">Laravel Excel</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#模型过滤"><span class="nav-number">57.</span> <span class="nav-text">模型过滤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-配置双模板"><span class="nav-number">58.</span> <span class="nav-text">Laravel 配置双模板</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#助手函数"><span class="nav-number">59.</span> <span class="nav-text">助手函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建无限极分类树"><span class="nav-number">60.</span> <span class="nav-text">创建无限极分类树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#任务重复执行"><span class="nav-number">61.</span> <span class="nav-text">任务重复执行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建一个命令"><span class="nav-number">62.</span> <span class="nav-text">创建一个命令</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自定义-Laravel-的-404-页面"><span class="nav-number">63.</span> <span class="nav-text">自定义 Laravel 的 404 页面</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图片处理"><span class="nav-number">64.</span> <span class="nav-text">图片处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#取消事件触发"><span class="nav-number">65.</span> <span class="nav-text">取消事件触发</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#记录memcache-mongodb运行时间"><span class="nav-number">66.</span> <span class="nav-text">记录memcache mongodb运行时间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#记录Redis运行时间"><span class="nav-number">67.</span> <span class="nav-text">记录Redis运行时间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#记录guggle运行时间"><span class="nav-number">68.</span> <span class="nav-text">记录guggle运行时间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#未加密的-Cookie"><span class="nav-number">69.</span> <span class="nav-text">未加密的 Cookie</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#无法获取-cookie"><span class="nav-number">70.</span> <span class="nav-text">无法获取 cookie</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-Excel-导出-csv"><span class="nav-number">71.</span> <span class="nav-text">Laravel Excel 导出 csv</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#项目开发规范"><span class="nav-number">72.</span> <span class="nav-text">项目开发规范</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-中-Facade"><span class="nav-number">73.</span> <span class="nav-text">Laravel 中 Facade</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-默认邮箱登录改成用户名登录"><span class="nav-number">74.</span> <span class="nav-text">Laravel 默认邮箱登录改成用户名登录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设置错误等级"><span class="nav-number">75.</span> <span class="nav-text">设置错误等级</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#图片添加水印"><span class="nav-number">76.</span> <span class="nav-text">图片添加水印</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-配置二级目录访问"><span class="nav-number">77.</span> <span class="nav-text">Laravel 配置二级目录访问</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Lumen-日志自定义"><span class="nav-number">78.</span> <span class="nav-text">Lumen 日志自定义</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Lumen-实时记录-SQL"><span class="nav-number">79.</span> <span class="nav-text">Lumen 实时记录 SQL</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#替换-Word-里面变量并导出-PDF"><span class="nav-number">80.</span> <span class="nav-text">替换 Word 里面变量并导出 PDF</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#主键进行批量更新"><span class="nav-number">81.</span> <span class="nav-text">主键进行批量更新</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laradock-中使用-beanstalkd"><span class="nav-number">82.</span> <span class="nav-text">Laradock 中使用 beanstalkd</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#注册了多少条路由"><span class="nav-number">83.</span> <span class="nav-text">注册了多少条路由</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-ORM-SQL-语句查询"><span class="nav-number">84.</span> <span class="nav-text">Laravel ORM SQL 语句查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cache"><span class="nav-number">85.</span> <span class="nav-text">cache</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-的请求是怎么到达控制器"><span class="nav-number">86.</span> <span class="nav-text">Laravel 的请求是怎么到达控制器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分类列表无限分类"><span class="nav-number">87.</span> <span class="nav-text">分类列表无限分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取路由"><span class="nav-number">88.</span> <span class="nav-text">获取路由</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#自动将空字符串转为-null"><span class="nav-number">89.</span> <span class="nav-text">自动将空字符串转为 null</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Laravel-5-5-升级到-6-0"><span class="nav-number">90.</span> <span class="nav-text">Laravel 5.5 升级到 6.0</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#API-和-后台接口分离"><span class="nav-number">91.</span> <span class="nav-text">API 和 后台接口分离</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#laravel路由分割"><span class="nav-number">92.</span> <span class="nav-text">laravel路由分割</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
